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.

On May 11, 4:10 pm, google torp <toppe...@gmail.com> wrote:
> 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
>
> On 11 Maj, 22:48, adrian <adrian...@gmail.com> wrote:
>
> > 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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to