Discussion:
How to set queryset on the second step of a form wizard?
rr
2008-08-20 06:38:00 UTC
Permalink
Hi Guru,

Depending on step 1's selection, I want to set the queryset of one of
the fields (a ModelChoiceField)

e.g.
class StepOneForm(forms.Form):
channelType =
forms.ModelChoiceField(queryset=ChannelType.objects.all())
class StepTwoForm(forms.Form):
channel = forms.ModelChoiceField()

class MyFormWizard(FormWizard):
def process_step(self, request, form, step):
if step==0:
self.get_form(1).channel.queryset =
form.cleaned_data['channelType'].channel_set.all()

But it doesn't seems working... how can a dynamic choice can be
achieved depending on selection of previous step?!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Rajesh Dhawan
2008-08-20 15:06:27 UTC
Permalink
Hi,
Post by rr
Depending on step 1's selection, I want to set the queryset of one of
the fields (a ModelChoiceField)
e.g.
channelType =
forms.ModelChoiceField(queryset=ChannelType.objects.all())
channel = forms.ModelChoiceField()
self.get_form(1).channel.queryset =
form.cleaned_data['channelType'].channel_set.all()
The process_step method is called only after the form is already
validated. So, as you found out, that's not going to work. Also, you
are trying to set the queryset on form #1 when step is 0. That's not
going to persist.
Post by rr
But it doesn't seems working... how can a dynamic choice can be
achieved depending on selection of previous step?!
Try overriding the get_form method instead of process_step. Also, you
will need to populate form #1's queryset when you are in step 1. So,
the code would be something like this:

def get_form(self, step, data=None):
form = super(MyFormWizard, self).get_form(step, data=data)
if step == 1:
# Get step #0's form and set form #1's queryset based on it
form0 = super(MyFormWizard, self).get_form(0, data=data)
form.fields['channel'].queryset =
form0.cleaned_data['channelType'].channel_set.all()
return form

-Rajesh D

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-20 16:57:50 UTC
Permalink
Thanks Rajesh

But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess

form0 = super(MyFormWizard, self).get_form(0, data=data)

only gives you a form with no data?!
Hi,
Depending onstep1's selection, I want tosetthequerysetof one of
the fields (a ModelChoiceField)
e.g.
   channelType =
forms.ModelChoiceField(queryset=ChannelType.objects.all())
   channel = forms.ModelChoiceField()
            self.get_form(1).channel.queryset=
form.cleaned_data['channelType'].channel_set.all()
The process_step method is called only after theformis already
validated. So, as you found out, that's not going to work. Also, you
are trying tosetthequerysetonform#1 whenstepis 0. That's not
going to persist.
But it doesn't seems working... how can a dynamic choice can be
achieved depending on selection of previousstep?!
Try overriding the get_form method instead of process_step. Also, you
will need to populateform#1'squerysetwhen you are instep1. So,
   form= super(MyFormWizard, self).get_form(step, data=data)
        # Getstep#0'sformandsetform#1'squerysetbased on it
                form0 = super(MyFormWizard, self).get_form(0, data=data)
               form.fields['channel'].queryset=
form0.cleaned_data['channelType'].channel_set.all()
    returnform
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Rajesh Dhawan
2008-08-20 20:25:22 UTC
Permalink
Post by rr
Thanks Rajesh
But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess
form0 = super(MyFormWizard, self).get_form(0, data=data)
only gives you a form with no data?!
Can you dpaste what you've so far?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Rajesh Dhawan
2008-08-20 20:59:25 UTC
Permalink
Post by rr
Thanks Rajesh
But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess
form0 = super(MyFormWizard, self).get_form(0, data=data)
only gives you a form with no data?!
I just dpasted something that will help you capture the channelType
from step 0 and then use it when the step 1 form is being created.

http://dpaste.com/hold/72763/

If that doesn't work, please dpaste your MyFormWizard code.

-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-21 05:42:56 UTC
Permalink
It works well. Thanks a lot.

But now another problem. It seems "def done " is not working. It loops
back to the step1 and def done is never called

And do you mind explain how your code works? I did a "print step"
before return form in the def get_from, it prints 0, 1, 0. It seems it
has been called 3 times when submit in the second step

What is the sequence of the call of process_step and get_form? the
docs didn't explain much about this

Thanks in Advance!!
-r
Post by Rajesh Dhawan
Post by rr
Thanks Rajesh
But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess
form0 = super(MyFormWizard, self).get_form(0, data=data)
only gives you aformwith no data?!
I just dpasted something that will help you capture the channelType
fromstep0 and then use it when thestep1formis being created.
http://dpaste.com/hold/72763/
If that doesn't work, please dpaste your MyFormWizard code.
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-21 05:51:56 UTC
Permalink
Don't worry. Form works now. Thanks a lot.

But would be great if you can explain it a bit on how your code works
and the sequence of how process_steps and get_form works as I see it
seems called a few time between steps

Thanks =)
-r
Post by rr
It works well. Thanks a lot.
But now another problem. It seems "def done " is not working. It loops
back to the step1 and def done is never called
And do you mind explain how your code works? I did a "printstep"
before returnformin the def get_from, it prints 0, 1, 0. It seems it
has been called 3 times when submit in thesecondstep
What is the sequence of the call of process_step and get_form? the
docs didn't explain much about this
Thanks in Advance!!
-r
Post by Rajesh Dhawan
Post by rr
Thanks Rajesh
But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess
form0 = super(MyFormWizard, self).get_form(0, data=data)
only gives you aformwith no data?!
I just dpasted something that will help you capture the channelType
fromstep0 and then use it when thestep1formis being created.
http://dpaste.com/hold/72763/
If that doesn't work, please dpaste your MyFormWizard code.
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-21 06:06:31 UTC
Permalink
sorry for being annoying

It doesn't work, complains about
'ChannelTypeForm' object has no attribute 'cleaned_data'
when submit on the second step(step1)
http://dpaste.com/72840/

-r
Don't worry.Formworks now. Thanks a lot.
But would be great if you can explain it a bit on how your code works
and the sequence of how process_steps and get_form works as I see it
seems called a few time between steps
Thanks =)
-r
Post by rr
It works well. Thanks a lot.
But now another problem. It seems "def done " is not working. It loops
back to the step1 and def done is never called
And do you mind explain how your code works? I did a "printstep"
before returnformin the def get_from, it prints 0, 1, 0. It seems it
has been called 3 times when submit in thesecondstep
What is the sequence of the call of process_step and get_form? the
docs didn't explain much about this
Thanks in Advance!!
-r
Post by Rajesh Dhawan
Post by rr
Thanks Rajesh
But...it doesn't work
It complains about form0 doesn't have cleaned_data. I guess
form0 = super(MyFormWizard, self).get_form(0, data=data)
only gives you aformwith no data?!
I just dpasted something that will help you capture the channelType
fromstep0 and then use it when thestep1formis being created.
http://dpaste.com/hold/72763/
If that doesn't work, please dpaste your MyFormWizard code.
-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Rajesh Dhawan
2008-08-21 13:39:58 UTC
Permalink
Post by rr
sorry for being annoying
It doesn't work, complains about
'ChannelTypeForm' object has no attribute 'cleaned_data'
when submit on the second step(step1)http://dpaste.com/72840/
Can you also dpaste your URLs.py that shows which form is step 0 and
step 1?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
J***@gmail.com
2008-08-21 14:05:43 UTC
Permalink
Override the process_step method do that when step one is being
processed, it creates the queryset to be used by step 2 then.

if(step==1):
if(form.is_valid()):
self.step2_queryset=Book.objects.filter(**form.cleaned_data)
elif(step==0):
self.step2_queryset=None

Then, override the get_form method to grab the self.step2_queryset
variable and use it in the form. Thus you don't have to worry about
cleaning forms more than once.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-21 15:56:35 UTC
Permalink
The url is here
http://dpaste.com/72961/

-r
Post by rr
sorry for being annoying
It doesn't work, complains about
'ChannelTypeForm' object has no attribute 'cleaned_data'
when submit on thesecondstep(step1)http://dpaste.com/72840/
Can you also dpaste your URLs.py that shows whichformisstep0 andstep1?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Rajesh Dhawan
2008-08-21 18:30:43 UTC
Permalink
The url is herehttp://dpaste.com/72961/
I don't see anything glaringly wrong. You mentioned that it "complains
about 'ChannelTypeForm' object has no attribute 'cleaned_data'"...can
you dpaste that error trace as well?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
rr
2008-08-22 23:03:27 UTC
Permalink
http://dpaste.com/73317/

I have dpasted it on the link above

Cheers
Roy
Post by Rajesh Dhawan
The url is herehttp://dpaste.com/72961/
I don't see anything glaringly wrong. You mentioned that it "complains
about 'ChannelTypeForm' object has no attribute 'cleaned_data'"...can
you dpaste that error trace as well?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...