mirror of
https://github.com/django/django.git
synced 2025-10-24 22:26:08 +00:00
Fixed #11905: Raise an error on model form creation if a non-existent field was listed in fields. Thanks ben and copelco.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13739 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -5,6 +5,7 @@ from django import forms
|
||||
from django.forms.models import modelform_factory, ModelChoiceField
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.core.exceptions import FieldError
|
||||
|
||||
from models import Person, RealPerson, Triple, FilePathModel, Article, \
|
||||
Publication, CustomFF, Author, Author1, Homepage
|
||||
@@ -294,3 +295,41 @@ class FormFieldCallbackTests(TestCase):
|
||||
self.assertRaises(TypeError, modelform_factory, Person,
|
||||
formfield_callback='not a function or callable')
|
||||
|
||||
|
||||
class InvalidFieldAndFactory(TestCase):
|
||||
""" Tests for #11905 """
|
||||
|
||||
def test_extra_field_model_form(self):
|
||||
try:
|
||||
class ExtraPersonForm(forms.ModelForm):
|
||||
""" ModelForm with an extra field """
|
||||
|
||||
age = forms.IntegerField()
|
||||
|
||||
class Meta:
|
||||
model = Person
|
||||
fields = ('name', 'no-field')
|
||||
except FieldError, e:
|
||||
# Make sure the exception contains some reference to the
|
||||
# field responsible for the problem.
|
||||
self.assertTrue('no-field' in e.args[0])
|
||||
else:
|
||||
self.fail('Invalid "no-field" field not caught')
|
||||
|
||||
def test_extra_declared_field_model_form(self):
|
||||
try:
|
||||
class ExtraPersonForm(forms.ModelForm):
|
||||
""" ModelForm with an extra field """
|
||||
|
||||
age = forms.IntegerField()
|
||||
|
||||
class Meta:
|
||||
model = Person
|
||||
fields = ('name', 'age')
|
||||
except FieldError:
|
||||
self.fail('Declarative field raised FieldError incorrectly')
|
||||
|
||||
def test_extra_field_modelform_factory(self):
|
||||
self.assertRaises(FieldError, modelform_factory,
|
||||
Person, fields=['no-field', 'name'])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user