Sunday 15 January 2012

python - how can I save a form with ModelMultipleChoiceField? -


I have a model calendar and in one form I want to be able to make it many examples.

Here are my models:

  class events (models.model): user = model. Fabric (user) class group (models.Model): name = models.CharField (_ ('name'), max_length = 80) event = model. Many Tom Manfield (via Event = 'Calendar') Class Calendar (Model.Model): Event = Model. Forininki (event) group = model. Forwarding (group) class calendar inline (Admin tabular inline): Model = Calendar Extra = 1 Class Group Administrator (Admin.ModelAdamin): Inline = = (CalendarInline)  

Here I want to code my form How do I try:

  class AddEventToGroupForm (ModelForm): Group = Model MultiTypeField (queryset = Group.objects.all), Widget = SelectMultiple ()) def Save (self): Live in self For: g.save () class meta: model = calendar field = ('group',)  

and this Not a part of my thoughts:

  email = Event.objects.get (id = event_id) calentry = calendar (event = e) if requested. POST: f = AddEventToGroupForm (Data = Request.POST, Example = Calendary) If f.is_valid (): f.save ()  

If I try to submit that form , Then I get:

  attributeError at / groups / add_event / 7 / 'BoundField' has no attribute in the object 'Save'  

This status What is the correct way to create multiple instances of calendars?

This form does not have to deal with many-to-many relationships. Can not read through again and save them, it does not really work in this way.

In this form, there is only one field, which happens with many values, here's the thing to do again through the values ​​ of this area, Get code> cleaned_data dictionary (when the form is valid).

So, in your opinion, you do something like this:

  if f.is_valid (): for group f.cleaned_data ['group']: calentry .groups.add (Group)  

Note that you are not 'saving' AddEventToGroupForm form. I will make it a standard form. Form, instead of a model form, because you are not really dependent on any model form functionality.


No comments:

Post a Comment