#24756: Unexpected behavior of ModelForm.save for fields with blank=False and
overridden required attribute
-------------------------------------+-------------------------------------
Reporter: bboogaard | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: forms models save | Triage Stage:
blank required | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I am not sure if we should support this use case because with `code=''`,
the submitted model is invalid from a model validation standpoint. Could
you elaborate on the use case to omit `blank=True` from the model field
definition but accept blank values on the form?
The relevant code in
[https://github.com/django/django/blob/4dcc6493418c78db07761180bf6265f5b2bbccbf/django/forms/models.py#L373-L383
forms/models.py].
Your proposed patch doesn't pass the test suite:
{{{
======================================================================
ERROR: test_blank_with_null_foreign_key_field
(model_forms.tests.ModelFormBaseTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tim/code/django/tests/model_forms/tests.py", line 231, in
test_blank_with_null_foreign_key_field
self.assertTrue(f1.is_valid())
File "/home/tim/code/django/django/forms/forms.py", line 168, in
is_valid
return self.is_bound and not self.errors
File "/home/tim/code/django/django/forms/forms.py", line 160, in errors
self.full_clean()
File "/home/tim/code/django/django/forms/forms.py", line 378, in
full_clean
self._post_clean()
File "/home/tim/code/django/django/forms/models.py", line 433, in
_post_clean
self.instance = construct_instance(self, self.instance, opts.fields,
construct_instance_exclude)
File "/home/tim/code/django/django/forms/models.py", line 60, in
construct_instance
f.save_form_data(instance, cleaned_data[f.name])
File "/home/tim/code/django/django/db/models/fields/__init__.py", line
856, in save_form_data
setattr(instance, self.name, data)
File "/home/tim/code/django/django/db/models/fields/related.py", line
634, in __set__
(instance._meta.object_name, self.field.name)
ValueError: Cannot assign None: "Student.character" does not allow null
values.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24756#comment:1>
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/067.d0130a0963eae3a7e25f955f7802ed64%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.