ModelChoiceField help

996 views
Skip to first unread message

Bobby Roberts

unread,
May 20, 2009, 11:00:23 AM5/20/09
to Django users
Hi Group. Please look at this snippet from my forms. It uses two
ModelChoiceFields as shown below. Each database table currently has 2
records.

On the webpage, when the form is being called, the options are like
this:

Template object
Template object

and

PageCategory object
PageCategory object

The values of the options are correct, but how do i get the correct
visible text in the drop box

from django import forms
from web_pages.models import PageCategory
from template_manager.models import Template

class FrmWebPage (forms.Form):
page_category_id = forms.ModelChoiceField
(queryset=PageCategory.objects, empty_label='Choose',required=True,
widget=forms.Select(attrs={'class':'dropbox'}))

template_id=forms.ModelChoiceField
(queryset=Template.objects,required=True,
empty_label='Choose',widget=forms.Select(attrs={'class':'dropbox'}))


def clean_page_category_id(self):
data=self.cleaned_data['page_category_id']
return data

def clean_template_id(self):
data=self.cleaned_data['template_id']
return data

Alex Gaynor

unread,
May 20, 2009, 11:09:30 AM5/20/09
to django...@googlegroups.com
Take a look at label_from_instance: http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Bobby Roberts

unread,
May 20, 2009, 11:38:06 AM5/20/09
to Django users
> Take a look at label_from_instance:http://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield
>
> Alex
>

yeah I'm lookin at that but it makes no sense to me.

Here's what i have and it's not working:

from django import forms
from web_pages.models import PageCategory
from template_manager.models import Template

class FrmWebPage (forms.Form):
page_category_id = forms.ModelChoiceField
(queryset=PageCategory.objects, empty_label='Choose',required=True,
widget=forms.Select(attrs={'class':'dropbox'}))

template_id=forms.ModelChoiceField
(queryset=Template.objects,required=True,
empty_label='Choose',widget=forms.Select(attrs={'class':'dropbox'}))


class MyModelChoiceField (page_category_id):
def label_from_instance(self,obj):
return "My Object #%i" % obj.id

Alex Gaynor

unread,
May 20, 2009, 12:03:02 PM5/20/09
to django...@googlegroups.com
You aren't actually using that Field though, you need to use it in place of ModelChoiceField where you want that behavior.

Bobby Roberts

unread,
May 20, 2009, 1:32:30 PM5/20/09
to Django users
> You aren't actually using that Field though, you need to use it in place of
> ModelChoiceField where you want that behavior.
>
> Alex


here's what i've got:

class FrmWebPage (forms.Form):
active = forms.ChoiceField(required=True, choices=active_choices,
widget=forms.RadioSelect(attrs={'class':'choiceselect'}))
page_category_id = forms.MyModelChoiceField
(queryset=PageCategory.objects, empty_label='Choose',required=True,
widget=forms.Select(attrs={'class':'dropbox'}))
on_rss = forms.ChoiceField(required=True, choices=active_choices,
widget=forms.RadioSelect(attrs={'class':'choiceselect'}))

tags=forms.CharField(max_length=254, required=False,
widget=forms.TextInput(attrs={'class':'reallylargetextbox'}))
url_slug = forms.SlugField (max_length=254, required=True,
widget=forms.TextInput(attrs={'class':'reallylargetextbox'}))
blurb = forms.CharField(max_length=254, required=False,
widget=forms.Textarea(attrs={'class':'mediumblob'}))

page_weight = forms.ChoiceField(required=True,
choices=weight_choices, widget=forms.Select(attrs=
{'class':'dropbox'}))
browser_title = forms.CharField(max_length=254, required=True,
widget=forms.TextInput(attrs={'class':'reallylargetextbox'}))
meta_keywords = forms.CharField(max_length=254, required=False,
widget=forms.TextInput(attrs={'class':'reallylargetextbox'}))
meta_description = forms.CharField(max_length=254, required=False,
widget=forms.TextInput(attrs={'class':'reallylargetextbox'}))
template_id=forms.ModelChoiceField
(queryset=Template.objects,required=True,
empty_label='Choose',widget=forms.Select(attrs={'class':'dropbox'}))
body = forms.CharField (required=True,widget=forms.Textarea(attrs=
{'cols':107,'rows':30}))

class MyModelChoiceField (page_category_id):
def label_from_instance(self,obj):
return "My Object #%i" % obj.id


When i run it, i get this traceback:

Error was: 'module' object has no attribute 'MyModelChoiceField'

any ideas? The docs aren't very helpful to someone new to python

Alex Gaynor

unread,
May 20, 2009, 1:55:39 PM5/20/09
to django...@googlegroups.com
You don't access the class at forms.MyModeChoiceField, since it doesn't live in that namespace.  You access it with just MyModelChocieField, since it's defined in the same file.  As a note, you're going to want to have the MyModelChoiceField class before your form (in the file), otherwise the Field won't be defined when you go to use it.

Bobby Roberts

unread,
May 20, 2009, 2:38:37 PM5/20/09
to Django users
> You don't access the class at forms.MyModeChoiceField, since it doesn't live
> in that namespace.  You access it with just MyModelChocieField, since it's
> defined in the same file.  As a note, you're going to want to have the
> MyModelChoiceField class before your form (in the file), otherwise the Field
> won't be defined when you go to use it.
>
> Alex



Alex -

I really appreciate your help. I made the changes you suggested and
it's shown here:

class MyModelChoiceField (ModelChoiceField):
def label_from_instance(self,obj):
return "My Object #%i" % obj.id

class FrmWebPage (forms.Form):
active = forms.ChoiceField(required=True, choices=active_choices,
widget=forms.RadioSelect(attrs={'class':'choiceselect'}))
page_category_id = MyModelChoiceField
(queryset=PageCategory.objects, empty_label='Choose',required=True,
widget=forms.Select(attrs={'class':'dropbox'}))

[snip]

page_weight = MyModelChoiceField(required=True,
choices=weight_choices, widget=forms.Select(attrs=
{'class':'dropbox'}))


and the result is:

name 'ModelChoiceField' is not defined

Alex Gaynor

unread,
May 20, 2009, 2:41:15 PM5/20/09
to django...@googlegroups.com
Your class should inherit from "forms.ModelChoiceField", not just ModelChocieField.

Bobby Roberts

unread,
May 20, 2009, 3:05:07 PM5/20/09
to Django users

> Your class should inherit from "forms.ModelChoiceField", not just
> ModelChocieField.
>
> Alex


Ok i'm getting somewhere! Thanks for your patience. I'm still
finding my python / django legs. This populates the dropbox with

My Object #1
My Object #2
My Object #3

I see why it's dong that in the override class but is there a way to
pull a field value from the database and put it in there?

In other words in my table i have:

id
url
name

i'm looking for a way to make the select like such:

<select>
<option value="1">Wide Template</option>
<option value="2">Home Page</option>
<option value="3">Eblast July 4</option>
</select>

where the option value is the id field and the visible text is the
name fiel

Alex Gaynor

unread,
May 20, 2009, 3:15:31 PM5/20/09
to django...@googlegroups.com
Sure, label_for_instance gets the object itself, with all it's fields.  So isntead of returning the string with the id number, change it to return: obj.name.

Bobby Roberts

unread,
May 20, 2009, 3:22:20 PM5/20/09
to Django users
> Sure, label_for_instance gets the object itself, with all it's fields.  So
> isntead of returning the string with the id number, change it to return:
> obj.name.
>
> Alex

PERFECT! Thanks a lot!
Reply all
Reply to author
Forward
0 new messages