#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 | Keywords: forms models save blank required
blocker |
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+----------------------------------------------
Since upgrading to Django 1.8.1, I'm getting unexpected behavior when
setting the required attribute for ModelForm fields to False for fields
with blank=False in their field declaration on the model. Contrary to
previous Django versions, the field is now ignored when calling the save()
method of the ModelForm.
The model:
{{{
from django.db import models
class CodeName(models.Model):
code = models.CharField('Code', max_length=20)
name = models.CharField('Name', max_length=100)
}}}
The form:
{{{
from django import forms
from .models import CodeName
class CodeNameForm(forms.ModelForm):
class Meta:
model = CodeName
fields = ['code', 'name']
def __init__(self, *args, **kwargs):
super(CodeNameForm, self).__init__(*args, **kwargs)
self.fields['code'].required = False
}}}
Failing test:
{{{
from django.test import TestCase
from .forms import CodeNameForm
from .models import CodeName
class TestCodeNameForm(TestCase):
def test_save(self):
instance = CodeName(code='foo', name='Foobar')
data = {
'code': '',
'name': 'Foobar'
}
form = CodeNameForm(data, instance=instance)
form.full_clean()
instance = form.save()
self.assertEqual(instance.code, '')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24756>
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.a3f81a69e0364dcd91842f57ae8e747c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.