List Info

Thread: ChoiceField: where to initialize choices




ChoiceField: where to initialize choices
country flaguser name
Germany
2007-02-28 08:07:12
Hello,

Considering this basic model:

******************************************************
*from django.contrib.auth.models import User
*
*class Meeting(models.Model):
*    chairmen = models.ManyToManyField(User,
related_name="meetings")
*    participants = models.ManyToManyField(User,
related_name="participants")
******************************************************

How could I build the form to create a new Meeting?
My problem is to initialize the MultipleChoiceField's
choices with the same list
of users for both chairmen and participants:

======================================================
=class MeetingForm(forms.Form):
=    chairmen =
forms.MultipleChoiceField(choices=[(u.id,u.name) for u in
User.objects.all()])
=    participants =
forms.MultipleChoiceField(choices=[(u.id,u.name) for u in
User.objects.all()])
======================================================

Not DRY enough to me, so I tried initializing choices in the
__init__():

======================================================
= (...)
= def __init__(self, *args, **kwargs):
=     super(MeetingForm, self).__init__(*args, **kwargs)
=     user_list = [(u.id,u.name) for u in
User.objects.all()]
=     self.fields['chairmen'].choices = user_list
=     self.fields['participants'].choices = user_list
======================================================

When displaying this (supposedly unbound) form, it complains
about validation
errors, with the luxury of not displaying the users in the
HTML output.
I could not find a proper answer to my problem in previous
topics on this list.

Any advice on my code would be much appreciated (I know I
could use
form_for_model, but I'm trying to learn newforms without its
black magic
shortcuts).

Kind regards.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )