database values in a select form.

53 views
Skip to first unread message

Lee Hinde

unread,
Jul 8, 2012, 3:25:54 AM7/8/12
to django...@googlegroups.com
I have a table, Choice, that is used to build common lists. For
instance, there might be a list "Employment_Status" with values
"Full-Time" and "Part-Time"

I want to use those values in various forms, so in the appropriate
models.py I have:

def _get_list_choices(list):
choices = Choice.on_site.filter(category =
list).values_list('choice', flat=True).order_by('sort_order')
return tuple(choices)


and then I'd use it like this:

class PersonForm(ModelForm):
household = forms.CharField(
widget=forms.HiddenInput())
employment_types = _get_list_choices('Employment_Status')
employment_status = ChoiceField(widget=forms.Select, choices =
zip(employment_types,employment_types))


What I'm noticing is that (in dev mode) the choice list for
'employment_status' doesn't update unless I trigger a change, either
to the forms.py or restart the dev server.

I'm wondering if there's a better place to handle this.

Thanks.

Michael Elkins

unread,
Jul 9, 2012, 2:39:16 AM7/9/12
to django...@googlegroups.com
On Sat, Jul 07, 2012 at 08:25:54PM -0700, Lee Hinde wrote:
>What I'm noticing is that (in dev mode) the choice list for
>'employment_status' doesn't update unless I trigger a change, either
>to the forms.py or restart the dev server.
>
>I'm wondering if there's a better place to handle this.

Lee,

Here are some examples of the solution to your problem:

http://ilian.i-n-i.org/django-forms-choicefield-with-dynamic-values/

http://stackoverflow.com/questions/3419997/creating-a-dynamic-choice-field

You can do a web search on "form choicefield dynamic" for other
discussions on the same topic.

me

Daniel Roseman

unread,
Jul 9, 2012, 1:07:54 PM7/9/12
to django...@googlegroups.com
The problem, as you've probably guessed, is that the _get_list_choices() function is called when the form is defined, which happens when its module is first imported during a process. The choices are allocated at that point, and don't change while the process lasts.

So instead you need to make sure the function is called every time the form is instantiated, which you can do in the __init__ method:

    class MyForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(MyForm, self).__init__(*args, **kwargs)
            self.fields['employment_status'].choices = _get_list_choices('Employment_Status') 

Alternatively, you might want to consider using a ModelChoiceField instead of a plain ChoiceField, and using the `queryset` parameter rather than `choices` - your method can then return the actual Choice objects, and the field will ensure that the queryset is evaluated each time.
--
DR. 

Lee Hinde

unread,
Jul 10, 2012, 1:56:13 AM7/10/12
to django...@googlegroups.com
Thanks.

I combined the two tips and have something that works. I appreciate the help.

And, I did google first, really. Just not the right keywords as it turns out...
Reply all
Reply to author
Forward
0 new messages