change query for ModelChoiceField

18 views
Skip to first unread message

adrian

unread,
May 11, 2009, 4:48:06 PM5/11/09
to Django users

I want to create a form with a select box populated from a query that
I pass from the view.

For example:


if country == 'Canada':
methods = ShippingMethod.objects.filter(
delivery_time__lte=time_avail_hours,
abbrev__contains='SC'
)
else:
methods = ShippingMethod.objects.filter(
delivery_time__lte=time_avail_hours,
abbrev__contains='SU'
)

formShip = ShippingMethodForm(initial={
'queryset': methods,
})

My Form is defined as:

class ShippingMethodForm(forms.Form):
ship_method = forms.ModelChoiceField
(queryset=ShippingMethod.objects.none())
def __init__(self, *args, **kwargs):
super(ShippingMethodForm, self).__init__(*args, **kwargs)
self.fields["ship_method"].queryset =
ShippingMethod.objects.filter(something)


However the queryset I define in the view is not being used by
ShippingMethodForm, without the __init__ method shown. I don't know
how to pass the "initial" argument to the init method. What is the
correct way to do this?

Thanks

google torp

unread,
May 11, 2009, 5:10:00 PM5/11/09
to Django users
This should fix it.

def __init__(self, something, *args, **kwargs):
super(ShippingMethodForm, self).__init__(*args, **kwargs)
self.fields["ship_method"].queryset =
ShippingMethod.objects.filter(something)

when you initiate the form, you do it like this:
form = ShippingMethodForm(something, request.POST)

~Jakob

adrian

unread,
May 11, 2009, 5:27:01 PM5/11/09
to Django users
I tried in the view:

filter_string = "delivery_time__lte=time_avail_hours,
abbrev__contains='SC'"
formShip = ShippingMethodForm(filter_string)

Then the form def is:

class ShippingMethodForm(forms.Form):
# tricky thing done here to change queryset based on ticket date
and destination
ship_method = forms.ModelChoiceField
(queryset=ShippingMethod.objects.none())
def __init__(self, *args, **kwargs):
super(ShippingMethodForm, self).__init__(*args, **kwargs)
self.fields["ship_method"].queryset =
ShippingMethod.objects.filter(filter_string)

Doesn't work, I get NameError ....global name 'filter_string' is not
defined

Also this is the GET portion of the view, so putting request.POST in
there is not appropriate.

adrian

unread,
May 11, 2009, 5:29:18 PM5/11/09
to Django users
oops! missing the filter_string arg in __init__. After I added that,
I get:

ValueError .....too many values to unpack

adrian

unread,
May 11, 2009, 6:14:50 PM5/11/09
to Django users

For the record, here's what works:

In views.py:

if country == 'Canada':
delivery_time__lte=time_avail_hours
abbrev__contains='SC'

formShip = ShippingMethodForm(delivery_time__lte, abbrev__contains)


Then in models.py:

class ShippingMethodForm(forms.Form):
# tricky thing done here to change queryset based on ticket date
and destination
ship_method = forms.ModelChoiceField
(queryset=ShippingMethod.objects.none())
def __init__(self, delivery_time__lte, abbrev__contains, *args,
**kwargs):
super(ShippingMethodForm, self).__init__(*args, **kwargs)
self.fields["ship_method"].queryset =
ShippingMethod.objects.filter(
delivery_time__lte=delivery_time__lte,
abbrev__contains=abbrev__contains
)


krylatij

unread,
May 12, 2009, 2:42:25 AM5/12/09
to Django users
def __init__(self, *args, **kwargs):
super(ShippingMethodForm, self).__init__(*args, **kwargs)
if self.fields['country'].initial == 'Canada':
filter_kwargs = { 'delivery_time__lte': time_avail_hours,
'abbrev__contains'='SC' }
elif: ...
else: ..
self.fields["ship_method"].queryset =
ShippingMethod.objects.filter(**filter_kwargs )

hope this helps
Reply all
Reply to author
Forward
0 new messages