#25349: ModelForm regression when setting None into a ForeignKey
--------------------------+------------------------------------
Reporter: jpaulett | Owner: haxoza
Type: Bug | Status: assigned
Component: Forms | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------+------------------------------------
Comment (by haxoza):
Hi! I was digging this issue for a few hours and here are my findings:
* I run the tests proposed above by jpaulett and they failed as it's
described
* I was trying to prepare a fix digging in `django/forms/models.py` but
with no luck
* then I discovered then it is a wider misbehaviour of model forms fields
with `required=False` and generated from model fields with `blank=False`
(below there is a test)
* I've tried to run git bisect but with no luck - however the sure thing
is the issue wasn't there in 1.7.10
Here's a test for more general issue I found:
models.py:
{{{
from django.db import models
class Writer(models.Model):
name = models.CharField(max_length=30, blank=False)
}}}
tests.py:
{{{
from django import forms
from django.test.testcases import TestCase
from .models import Writer
class CustomWriterForm(forms.ModelForm):
name = forms.CharField(required=False)
class Meta(object):
model = Writer
fields = '__all__'
class ModelFormTest(TestCase):
def test_save_blank_false_with_required_false(self):
obj = Writer.objects.create(name='test')
value = ''
form = CustomWriterForm(data={'name': value}, instance=obj)
self.assertTrue(form.is_valid())
obj = form.save()
self.assertEqual(obj.name, value) # ASSERTION FAILS
}}}
When running the test in master branch:
{{{
$ python tests/runtests.py
aaaaa.tests.ModelFormTest.test_save_blank_false_with_required_false
Testing against Django installed in
'/Users/haxoza/dev/projects/django/django' with up to 4 processes
Creating test database for alias 'default'...
Creating test database for alias 'other'...
F
======================================================================
FAIL: test_save_blank_false_with_required_false
(aaaaa.tests.ModelFormTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/haxoza/dev/projects/django/tests/aaaaa/tests.py", line 24,
in test_save_blank_false_with_required_false
self.assertEqual(obj.name, value)
AssertionError: 'test' != ''
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (failures=1)
Destroying test database for alias 'default'...
Destroying test database for alias 'other'...
}}}
The test fails and I think what should actually happen is that the model
should be saved with empty string value.
In my opinion it should be reported as a separate issue and referenced to
this one because it looks quite serious.
--
Ticket URL: <https://code.djangoproject.com/ticket/25349#comment:5>
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/066.884eeed7abdc2b5720399ae0b81ba750%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.