#18172: Admin form fails to generate proper ModelMultipleChoiceField when using iterable model -------------------------------+-------------------- Reporter: eric.olstad@… | Owner: nobody Type: Uncategorized | Status: new Component: Forms | Version: 1.4 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+-------------------- When using default admin forms for a model with a ManyToManyField to an iterable model (defines {{{__iter__}}}), the select control generated will have options that have list values instead of the primary key on the model.
Example: {{{ class MyIterableModel(models.Model): def __iter__(self): for item in self.items: yield item class MyItem(models.Model): owner = models.ForeignKey(MyIterableModel, related_name=items) class MyOtherModel(models.Model): myiterables = model.ManyToManyField(MyIterableModel) }}} In the admin: {{{ admin.site.register(models.MyOtherModel) }}} When viewing the MyOtherModel page in the admin, the control for the ManyToManyField will have lists of primary keys as each option value. {{{ <select multiple="multiple" name="myiterablemodels" id="id_myiterablemodels"> <option value="[35636, 35383]">Yada Yada</option> </select> }}} This of course generates "not a valid value for primary key" when submitting. This is because django/forms/models.py prepare_value returns a list. {{{ 1035 def prepare_value(self, value): 1036 if hasattr(value, '__iter__'): 1037 return [super(ModelMultipleChoiceField, self).prepare_value(v) for v in value] 1038 return super(ModelMultipleChoiceField, self).prepare_value(value) }}} I'm not sure why prepare_value returns a list in that case, so I don't have suggestions for a fix. The workaround is to not use iterable models. -- Ticket URL: <https://code.djangoproject.com/ticket/18172> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.