Dynamic queryset to ModelChoiceField

133 views
Skip to first unread message

shabda

unread,
Apr 15, 2008, 12:14:58 AM4/15/08
to Django users
I have a form where I want to do something like,

class SetDefaultForm(forms.Form):
topics = forms.ModelChoiceField()

def __init__(self, user):
super(SetDefaultForm, self).__init__()
self.fields['topics'].queryset = Topic.objects.filter(user)

This does not work, of course, as ModelChoiceField has queryset as
required argument.

So I have to do something like,

class SetDefaultForm(forms.Form):
topics = forms.ChoiceField()

def __init__(self, user):
super(SetDefaultForm, self).__init__()
self.fields['topics'].choices= [topic for topic in
Topic.objects.filter(user)]

Is there a better way?

alex....@gmail.com

unread,
Apr 15, 2008, 12:46:32 AM4/15/08
to Django users
Sure, just give the modelchoicefield Topic.objects.all() to start
with, as this will never actually get used.

Alex Koshelev

unread,
Apr 15, 2008, 9:40:14 AM4/15/08
to Django users
Or `Topic.objects.none()`

On Apr 15, 8:46 am, "alex.gay...@gmail.com" <alex.gay...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages