#30376: Saving ModelForm with missing, non-optional fields does not trigger a
validation error
-----------------------------------------+------------------------
               Reporter:  Will Gordon    |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  Uncategorized  |        Version:  2.1
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 I have my model

 {{{#!python
 class Meeting(models.Model):
     title = models.CharField(max_length=255, help_text='A short header for
 your meeting')
     summary = models.TextField(help_text='An overview of the meeting.
 You\'ll have the opportunity to add specific notes later on')
     outcome = models.TextField(help_text='What was the final conclusion to
 the meeting')
     attended = models.DateField(help_text='When did you attend this
 meeting')
     attendees = models.ManyToManyField(Person, related_name='meetings')
     companies = models.ManyToManyField(Company, related_name='meetings')
     industries = models.ManyToManyField(Industry, related_name='meetings')
     author = models.ForeignKey(Person, on_delete=models.SET_NULL,
 related_name='my_meetings', null=True)
 }}}

 and my form

 {{{#!python
 class MeetingForm(forms.ModelForm):
     class Meta:
         model = models.Meeting
         fields = ['author', 'attended', 'attendees', 'companies',
 'industries', 'outcome']
 }}}

 Because `title`, and `summary` are not listed in my form's `fields`, I
 would expect this form to never be savable.

 This was a result of my merging of two models, I updated the Model, but
 not the Form. I was surprised when I was then able to save the Form.

 Based on documentation from
 https://docs.djangoproject.com/en/2.1/topics/forms/modelforms/#selecting-
 the-fields-to-use

   Django will prevent any attempt to save an incomplete model, so if the
 model does not allow the missing fields to be empty, and does not provide
 a default value for the missing fields, any attempt to save() a ModelForm
 with missing fields will fail.

 I would expect the validation to fail since there is no default value
 provided.

 This is either a docs bug, with the form validation working as expected
 (not validating any fields explicitly excluded)...or a form validation
 bug, where the form should still validate all Model fields to ensure it's
 not saving blank fields.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30376>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.768e2013f4140902dcac8cc6f2f02980%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to