ModelChoiceField with Unique Results

2008-09-22 Thread BobZ

What I'm trying to do seems relatively simple, but I have yet to find
a proper solution for it.

I'm trying to query a list of years from a database of registered
vehicles in my county and display them in a drop-down select menu in a
form.

Since the registered vehicles database has many  cars of the same
year,  I need to make those results from the query display in a unique
(no duplicate 2007 options for example), descending order when the
select menu is clicked.

Here's what I've been using so far in my forms.py file:
#class SearchForm(forms.ModelForm):
#year = forms.ModelChoiceField
#class Meta:
#model = Vehicle

This only gives me an empty text field.
I'm fairly new to Django, so any help would be greatly appreciated.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ModelChoiceField with Unique Results

2008-09-22 Thread BobZ

Thanks dmorozov, that worked fine in the sense that it returned only
unique years in a select box, but it still didn't order them properly
(getting non-duplicate years as 1961, 1931, 2000, 1975, 1995, etc.).

Somehow the order_by section of  "set([(obj.year, obj.year) for obj in
Vehicle.objects.all().order_by('-year')]) " isn't performing its
function.

Any ideas?

Thanks again!

On Sep 22, 1:39 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> You can try something like this:
>
> class SearchForm(forms.ModelForm):
>     year = forms.ChoiceField()
>
>     def __init__(self, *args, **kwargs):
>         super(SearchForm, self) .__init__(*args, **kwargs)
>
>         self.fields['year'].choices = \
>             set([(obj.year, obj.year) for obj in \
>             Vehicle.objects.all().order_by('-year')])
>
>     class Meta:
>         model = Vehicle
>
> On Sep 22, 8:17 pm, BobZ <[EMAIL PROTECTED]> wrote:
>
> > What I'm trying to do seems relatively simple, but I have yet to find
> > a proper solution for it.
>
> > I'm trying to query a list of years from a database of registered
> > vehicles in my county and display them in a drop-down select menu in a
> > form.
>
> > Since the registered vehicles database has many  cars of the same
> > year,  I need to make those results from the query display in a unique
> > (no duplicate 2007 options for example), descending order when the
> > select menu is clicked.
>
> > Here's what I've been using so far in my forms.py file:
> > #class SearchForm(forms.ModelForm):
> > #    year = forms.ModelChoiceField
> > #    class Meta:
> > #        model = Vehicle
>
> > This only gives me an empty text field.
> > I'm fairly new to Django, so any help would be greatly appreciated.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ModelChoiceField with Unique Results

2008-09-23 Thread BobZ

Thanks Daniel.  I've found solutions similar to yours in other threads
on the net, and everytime I test them, I literally get no form at all
in my rendered template.
My template appears to have all the correct code in it as you can see
in the link "search.html" at the bottom of this post.
Looking at the code you provided, it seems like the
"queryset=Vehicle.objects.order_by('-year').distinct()" pulls all
distinct fields from the Vehicle table and sorts them by year.  What
I'm actually after is a way to filter out only the years and list them
in descending order.

In my forms.py file, I've kept all variations of the code I've tested
for this app commented out for my own reference, but maybe it will
help you see what's happening and what I'm trying to do.

Here's all of my code in dpaste:
http://dpaste.com/80010/";>models.py - http://dpaste.com/80010/
http://dpaste.com/80009/";>forms.py - http://dpaste.com/80009/
http://dpaste.com/80011/";>views.py - http://dpaste.com/80011/
http://dpaste.com/80013/";>search.html - http://dpaste.com/80013/

On Sep 23, 1:32 am, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Sep 22, 10:58 pm, BobZ <[EMAIL PROTECTED]> wrote:
>
> > Thanks dmorozov, that worked fine in the sense that it returned only
> > unique years in a select box, but it still didn't order them properly
> > (getting non-duplicate years as 1961, 1931, 2000, 1975, 1995, etc.).
>
> > Somehow the order_by section of  "set([(obj.year, obj.year) for obj in
> > Vehicle.objects.all().order_by('-year')]) " isn't performing its
> > function.
>
> > Any ideas?
>
> > Thanks again!
>
> To be honest, dmorozov's solution sounds like overkill for what you
> need. Try something like this:
>
> class SearchForm(forms.ModelForm):
>     year = forms.ModelChoiceField(queryset=Vehicle.objects.order_by('-
> year').distinct())
>
> You'll need to be sure Vehicle has a __unicode__ method for this to
> work.
> --
> DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ModelChoiceField with Unique Results

2008-09-23 Thread BobZ

I had SOME luck with "queryset=Vehicle.objects.filter(year=True)" in
that the form actually rendered in my template, but the year select
box is empty now.  All other select boxes load fine (Make and Model)
with their data...only year still won't work.

On Sep 23, 10:44 am, BobZ <[EMAIL PROTECTED]> wrote:
> Thanks Daniel.  I've found solutions similar to yours in other threads
> on the net, and everytime I test them, I literally get no form at all
> in my rendered template.
> My template appears to have all the correct code in it as you can see
> in the link "search.html" at the bottom of this post.
> Looking at the code you provided, it seems like the
> "queryset=Vehicle.objects.order_by('-year').distinct()" pulls all
> distinct fields from the Vehicle table and sorts them by year.  What
> I'm actually after is a way to filter out only the years and list them
> in descending order.
>
> In my forms.py file, I've kept all variations of the code I've tested
> for this app commented out for my own reference, but maybe it will
> help you see what's happening and what I'm trying to do.
>
> Here's all of my code in dpaste:
> http://dpaste.com/80010/";>models.py -http://dpaste.com/80010/
> http://dpaste.com/80009/";>forms.py -http://dpaste.com/80009/
> http://dpaste.com/80011/";>views.py -http://dpaste.com/80011/
> http://dpaste.com/80013/";>search.html -http://dpaste.com/80013/
>
> On Sep 23, 1:32 am, Daniel Roseman <[EMAIL PROTECTED]>
> wrote:
>
> > On Sep 22, 10:58 pm, BobZ <[EMAIL PROTECTED]> wrote:
>
> > > Thanks dmorozov, that worked fine in the sense that it returned only
> > > unique years in a select box, but it still didn't order them properly
> > > (getting non-duplicate years as 1961, 1931, 2000, 1975, 1995, etc.).
>
> > > Somehow the order_by section of  "set([(obj.year, obj.year) for obj in
> > > Vehicle.objects.all().order_by('-year')]) " isn't performing its
> > > function.
>
> > > Any ideas?
>
> > > Thanks again!
>
> > To be honest, dmorozov's solution sounds like overkill for what you
> > need. Try something like this:
>
> > class SearchForm(forms.ModelForm):
> >     year = forms.ModelChoiceField(queryset=Vehicle.objects.order_by('-
> > year').distinct())
>
> > You'll need to be sure Vehicle has a __unicode__ method for this to
> > work.
> > --
> > DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: ModelChoiceField with Unique Results

2008-09-24 Thread BobZ

Wow, thank you so much Daniel.

I totally get this now, and it works!

Looks like I've sort of been skirting around this solution for a while
but, being clueless as I can be sometimes with Django, I never knew
what I could've been doing wrong.

Thanks a lot for the help again.

-Bob

On Sep 23, 4:37 pm, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Sep 23, 4:44 pm, BobZ <[EMAIL PROTECTED]> wrote:
>
>
>
> > Thanks Daniel.  I've found solutions similar to yours in other threads
> > on the net, and everytime I test them, I literally get no form at all
> > in my rendered template.
> > My template appears to have all the correct code in it as you can see
> > in the link "search.html" at the bottom of this post.
> > Looking at the code you provided, it seems like the
> > "queryset=Vehicle.objects.order_by('-year').distinct()" pulls all
> > distinct fields from the Vehicle table and sorts them by year.  What
> > I'm actually after is a way to filter out only the years and list them
> > in descending order.
>
> > In my forms.py file, I've kept all variations of the code I've tested
> > for this app commented out for my own reference, but maybe it will
> > help you see what's happening and what I'm trying to do.
>
> > Here's all of my code in dpaste:
> > http://dpaste.com/80010/";>models.py -http://dpaste.com/80010/
> > http://dpaste.com/80009/";>forms.py -http://dpaste.com/80009/
> > http://dpaste.com/80011/";>views.py -http://dpaste.com/80011/
> > http://dpaste.com/80013/";>search.html -http://dpaste.com/80013/
>
> I think you're misunderstanding the point of ModelForms.
>
> A ModelForm (as opposed to a standard form) is specifically for
> creating or editing a model instance. So if you had a model with a
> foreignkey to Vehicle, you could use this model as the base for a
> modelform with the modelchoice field we have defined.
>
> However, you have defined a ModelForm based on the Vehicle model, with
> a modelchoice field also pointing at Vehicle. That doesn't make sense,
> as the year field on Vehicle isn't a ForeignKey, it's an IntegerField.
> This could be why you're not getting any output in the rendered
> template.
>
> Luckily, your use case doesn't warrant a ModelForm anyway. You're not
> editing a model, you are using the form to create a search box. So you
> just want a standard Form. In which case, something akin to dmorozov's
> solution may be best after all. How about this:
>
> class SearchForm(forms.Form):
>     year=forms.ChoiceField()
>
>     def __init__(self, *args, **kwargs):
>         super(SearchForm, self) .__init__(*args, **kwargs)
>         self.fields['year'].choices =
> Vehicle.objects.values_list('year', 'year').distinct().order_by('-
> year')
>
> --
> DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to use form_class with generic views?

2009-08-24 Thread BobZ

I'm having a similar problem actually.

I'm trying to extend Photologue to offer front-end user upload
capability with generic views and I haven't been able to successfully
render a page yet.
Each of the commented-out "upload_args" are variations I've tried to
get something back and none of them have worked.

urls.py
#upload_args = {'model': Photo }
#upload_args = {'model': Photo, 'template_name':'photologue/
photo_upload.html' }
#upload_args = {'form_class':UserPhotoUploadForm }
#upload_args = {'form_class':UserPhotoUploadForm,
'template_name':'photologue/photo_upload.html' }
upload_args = dict(form_class=UserPhotoUploadForm)

urlpatterns += patterns('django.views.generic.create_update',
url(r'^photo/upload/$', 'create_object', upload_args ),
)

I continue to get this kind of cryptic error:

Page not found (404)
Request Method: GET
Request URL:http://localhost/photologue/photo/upload/

No  found
matching the query

Any help or guidance would be greatly appreciated!  :o)

On Jul 27, 7:26 pm, djangonoob  wrote:
> hi,
> in my urls.py i have imported the MyFormClass from models.py
>
> However i am still receiving the error  'NoneType object is not
> callable'
>
> What else do i need to import?
>
> Here is the things i have imported in urls.py
>
> #urls.py
> from django.conf.urls.defaults import *
> from django.conf import settings
> from mycompany.press.models import MyForm, MyFormClass
> from mycompany.press import views
> import datetime
> import os
> from django.forms import ModelForm
> from django.views.genericimport create_update
>
> Best Regards
>
> On Jul 27, 7:07 pm, Vasil Vangelovski  wrote:
>
> > First, of course you should separate that code, and form classes usually
> > go in forms.py, models in models.py.
> > Second, you are getting a 'NoneType object is not callable' beacause of
> > what you have in urls.py. Look at that carefully and you'll figure it out.
>
> > djangonoob wrote:
> > > oh by the way,
> > > i've tried your method and i received the error:
>
> > > 'NoneType' object is not callable
>
> > > May i know how do i solve this issue?
>
> > > Best Regards,
> > > Eugene
>
> > > On Jul 27, 12:15 pm, Vasil Vangelovski  wrote:
>
> > >> First you create a ModelForm:
>
> > >> from django.forms import ModelForm
> > >> from myproject.myapp.models import MyModelClass
>
> > >> class MyFormClass(ModelForm):
> > >> class Meta:
> > >> model = MyModelClass
>
> > >> there you have your form_class that you need to pass to the view you 
> > >> want:
>
> > >> ‘django.views.generic.create_update.update_object’,
> > >> dict(form_class=MyFormClass)
>
> > >> see:
>
> > >>http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#topics-...
>
> > >>> Hi all,
> > >>> I am wondering how can i use form_class withgenericviews?
>
> > >>> FOr example, according to the online 
> > >>> doc,http://docs.djangoproject.com/en/dev/ref/generic-views/
> > >>> we can use form_class as a required argument forcreate_object,
> > >>> update_object, delete_object
>
> > >>> But the online doc stopped short on how to use the form_class.
>
> > >>> Is there anyone woh has a simple example on how to use form_class as a
> > >>> required argument forgenericviews?
>
> > >>> Thanks alot!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to use form_class with generic views?

2009-08-24 Thread BobZ

Thanks for the quick response!

forms.py

from django import forms
from django.forms import ModelForm
from models import Photo

class UserPhotoUploadForm(ModelForm):
def __init__(self, *args, **kwargs ):
self.user = kwargs.pop("user")
super(UserPhotoUploadForm, self).__init__(*args, **kwargs)

class Meta:
model = Photo
exclude = ( 'user', 'title_slug', 'view_count',
'date_added', )

On Aug 24, 10:57 am, Daniel Roseman  wrote:
> On Aug 24, 4:26 pm, BobZ  wrote:
>
>
>
> > I'm having a similar problem actually.
>
> > I'm trying to extend Photologue to offer front-end user upload
> > capability with generic views and I haven't been able to successfully
> > render a page yet.
> > Each of the commented-out "upload_args" are variations I've tried to
> > get something back and none of them have worked.
>
> > urls.py
> > #upload_args = {'model': Photo }
> > #upload_args = {'model': Photo, 'template_name':'photologue/
> > photo_upload.html' }
> > #upload_args = {'form_class':UserPhotoUploadForm }
> > #upload_args = {'form_class':UserPhotoUploadForm,
> > 'template_name':'photologue/photo_upload.html' }
> > upload_args = dict(form_class=UserPhotoUploadForm)
>
> > urlpatterns += patterns('django.views.generic.create_update',
> >     url(r'^photo/upload/$', 'create_object', upload_args ),
> > )
>
> > I continue to get this kind of cryptic error:
>
> > Page not found (404)
> > Request Method:         GET
> > Request URL:    http://localhost/photologue/photo/upload/
>
> > No  found
> > matching the query
>
> > Any help or guidance would be greatly appreciated!  :o)
>
> Sounds like there's a problem with your form definition. Can you post
> that?
> --
> DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to use form_class with generic views?

2009-08-24 Thread BobZ

The real brain buster is that I get this error whether I'm passing a
form_class or a simple Model in urls.py

On Aug 24, 11:05 am, BobZ  wrote:
> Thanks for the quick response!
>
> forms.py
>
> from django import forms
> from django.forms import ModelForm
> from models import Photo
>
> class UserPhotoUploadForm(ModelForm):
>     def __init__(self, *args, **kwargs ):
>         self.user = kwargs.pop("user")
>         super(UserPhotoUploadForm, self).__init__(*args, **kwargs)
>
>     class Meta:
>         model = Photo
>         exclude = ( 'user', 'title_slug', 'view_count',
> 'date_added', )
>
> On Aug 24, 10:57 am, Daniel Roseman  wrote:
>
> > On Aug 24, 4:26 pm, BobZ  wrote:
>
> > > I'm having a similar problem actually.
>
> > > I'm trying to extend Photologue to offer front-end user upload
> > > capability with generic views and I haven't been able to successfully
> > > render a page yet.
> > > Each of the commented-out "upload_args" are variations I've tried to
> > > get something back and none of them have worked.
>
> > > urls.py
> > > #upload_args = {'model': Photo }
> > > #upload_args = {'model': Photo, 'template_name':'photologue/
> > > photo_upload.html' }
> > > #upload_args = {'form_class':UserPhotoUploadForm }
> > > #upload_args = {'form_class':UserPhotoUploadForm,
> > > 'template_name':'photologue/photo_upload.html' }
> > > upload_args = dict(form_class=UserPhotoUploadForm)
>
> > > urlpatterns += patterns('django.views.generic.create_update',
> > >     url(r'^photo/upload/$', 'create_object', upload_args ),
> > > )
>
> > > I continue to get this kind of cryptic error:
>
> > > Page not found (404)
> > > Request Method:         GET
> > > Request URL:    http://localhost/photologue/photo/upload/
>
> > > No  found
> > > matching the query
>
> > > Any help or guidance would be greatly appreciated!  :o)
>
> > Sounds like there's a problem with your form definition. Can you post
> > that?
> > --
> > DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: how to use form_class with generic views?

2009-08-25 Thread BobZ

Does anyone else have an idea of what could possibly cause this type
of error?

No  found
matching the query

I'm not looking for a spoonfed solution, just an idea of why this type
of error occurs.

Thanks for all your help,

Bob


On Aug 24, 1:24 pm, Daniel Roseman  wrote:
> On Aug 24, 5:06 pm, BobZ  wrote:
>
>
>
> > The real brain buster is that I get this error whether I'm passing a
> > form_class or a simple Model in urls.py
>
> > On Aug 24, 11:05 am, BobZ  wrote:
>
> > > Thanks for the quick response!
>
> > > forms.py
>
> > > from django import forms
> > > from django.forms import ModelForm
> > > from models import Photo
>
> > > class UserPhotoUploadForm(ModelForm):
> > >     def __init__(self, *args, **kwargs ):
> > >         self.user = kwargs.pop("user")
> > >         super(UserPhotoUploadForm, self).__init__(*args, **kwargs)
>
> > >     class Meta:
> > >         model = Photo
> > >         exclude = ( 'user', 'title_slug', 'view_count',
> > > 'date_added', )
>
> The only thing I can think of is that you're somehow redefining Photo
> somewhere, so that it's not actually a model. I can't think what would
> give that specific error, though.
> --
> DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---