Hi Tom,
-- I have not understood what you have said. Shouldn't choice_desig be called every time I instantiate it in the view with the form = New_user_form(). I thought I was creating an object of New_user_form every time I do that.
Regards,
Preetam
On Wednesday, 19 March 2014 22:11:52 UTC+5:30, sashank reddy wrote:
On Wednesday, 19 March 2014 22:11:52 UTC+5:30, sashank reddy wrote:
Hi Tom,What do you mean callable?I tried changingdesig_group = forms.ChoiceField(choices=choice_desig()) todesig_group = forms.ChoiceField(choices=choice_desig) It gave error 'function' object is not iterable.But I want to know if the following would work.desig = forms.ModelChoiceField(queryset=Group.objects.all()) But how do I change my view and template for it.Can you please correct my view and template.Regards,PreetamOn Wednesday, 19 March 2014 21:43:39 UTC+5:30, Tom Evans wrote:On Wed, Mar 19, 2014 at 3:40 PM, sashank reddy
<preetamsa...@gmail.com> wrote:
> Hi,
>
> I have been trying to extend the UserCreationForm and have a form which
> performs user registration and adds email and the names.
> In addition I am using a choice field that allows a list down of existing
> groups so that you can assign the user to an group immediately upon
> creation.
>
> The problem is that, if I create a new group, and try to add users to it,
> then the form validation fails.
> I have restart the django server or wait for it to reset internally and show
> "Validating models ..". Then it starts to work.
>
> The following is the code snippet:
> ============================================================ =================
> def choice_desig():
> choice_desig = []
> g = Group.objects.all()
> for k in g:
> choice_desig.append((k.name, k.name))
> return choice_desig
>
> class New_user_form(UserCreationForm):
> email = forms.EmailField(required=True)
> first_name = forms.CharField(required=True)
> last_name = forms.CharField(required=True)
> desig_group = forms.ChoiceField(choices=choice_desig())
If you use choice_desig() function like that, it will get called once,
when the server initially parses that file. It will never be run again
until the server re-parses that file.
You could pass a callable to choices - "choices=choice_desig".
However, the right solution for when you want a form field to choose a
model instance is to use a form.ModelChoiceField.
Cheers
Tom
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3392e13f-6933-4825-b3c0-e672c39e2a13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment