Hi Scot, On 11/05/2014 12:11 PM, Scot Hacker wrote: > I'm having a strange issue with a pair of ManyToManyFields, where > they're acting like they're required fields in the admin even though > they're not. > > > # In models.py > | > instructors =models.ManyToManyField(Instructor,blank=True) > students =models.ManyToManyField(Profile,blank=True) > > | > > > I can update an instance of this model with no instructors or students > from the CLI just fine. The fields are not required. But I'm using > ModelMultipleChoiceField in the admin: > > > # In admin.py > | > classOfferingAdminForm(forms.ModelForm): > > instructors =forms.ModelMultipleChoiceField( > widget > =widgets.FilteredSelectMultiple('Instructors',is_stacked=False), > queryset =Instructor.objects.all()) > > > students =forms.ModelMultipleChoiceField( > widget =widgets.FilteredSelectMultiple('Students',is_stacked=False), > queryset =User.objects.filter(is_active=True)) > | > > and when I try to save via the admin, it tells me these fields are > required. If I tweak it to not use this ModelForm, I can save without > instructors or students just fine.
When you override a field in a ModelForm, you lose the ability for it to automatically pick up configuration from the model field. Either the field is auto-created by the ModelForm based on your model field, or it is created manually by you; it can't be both. So because you are manually creating your `instructors` and `students` fields on the form, the `blank=True` in the model has no effect; you need to pass `required=False` to your two form fields. (Because of this, I frequently choose to update/tweak form fields in an overridden `__init__` method of a ModelForm, rather than overriding the fields entirely.) Carl -- 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+unsubscr...@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/545A773C.8080200%40oddbird.net. For more options, visit https://groups.google.com/d/optout.