2009-05-02 15:03:33 +08:00
|
|
|
from datetime import date
|
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
from django import forms
|
2010-10-01 10:02:58 +08:00
|
|
|
from django.core.exceptions import FieldError, ValidationError
|
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2010-12-04 15:28:12 +08:00
|
|
|
from django.forms.models import (modelform_factory, ModelChoiceField,
|
|
|
|
fields_for_model, construct_instance)
|
|
|
|
from django.utils import unittest
|
|
|
|
from django.test import TestCase
|
2009-05-02 15:03:33 +08:00
|
|
|
|
2010-08-05 11:59:20 +08:00
|
|
|
from models import Person, RealPerson, Triple, FilePathModel, Article, \
|
2010-10-13 12:36:51 +08:00
|
|
|
Publication, CustomFF, Author, Author1, Homepage, Document, Edition
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
|
|
|
|
class ModelMultipleChoiceFieldTests(TestCase):
|
|
|
|
def test_model_multiple_choice_number_of_queries(self):
|
|
|
|
"""
|
|
|
|
Test that ModelMultipleChoiceField does O(1) queries instead of
|
|
|
|
O(n) (#10156).
|
|
|
|
"""
|
|
|
|
for i in range(30):
|
|
|
|
Person.objects.create(name="Person %s" % i)
|
2009-05-02 15:03:33 +08:00
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
f = forms.ModelMultipleChoiceField(queryset=Person.objects.all())
|
2010-10-12 11:33:19 +08:00
|
|
|
self.assertNumQueries(1, f.clean, [1, 3, 5, 7, 9])
|
2009-04-18 23:51:11 +08:00
|
|
|
|
2010-12-13 06:57:37 +08:00
|
|
|
def test_model_multiple_choice_run_validators(self):
|
|
|
|
"""
|
|
|
|
Test that ModelMultipleChoiceField run given validators (#14144).
|
|
|
|
"""
|
|
|
|
for i in range(30):
|
|
|
|
Person.objects.create(name="Person %s" % i)
|
|
|
|
|
|
|
|
self._validator_run = False
|
|
|
|
def my_validator(value):
|
|
|
|
self._validator_run = True
|
|
|
|
|
|
|
|
f = forms.ModelMultipleChoiceField(queryset=Person.objects.all(),
|
|
|
|
validators=[my_validator])
|
2010-12-19 01:37:19 +08:00
|
|
|
|
|
|
|
f.clean([p.pk for p in Person.objects.all()[8:9]])
|
2010-12-13 06:57:37 +08:00
|
|
|
self.assertTrue(self._validator_run)
|
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
class TripleForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Triple
|
|
|
|
|
|
|
|
class UniqueTogetherTests(TestCase):
|
|
|
|
def test_multiple_field_unique_together(self):
|
|
|
|
"""
|
|
|
|
When the same field is involved in multiple unique_together
|
|
|
|
constraints, we need to make sure we don't remove the data for it
|
|
|
|
before doing all the validation checking (not just failing after
|
|
|
|
the first one).
|
|
|
|
"""
|
|
|
|
Triple.objects.create(left=1, middle=2, right=3)
|
|
|
|
|
|
|
|
form = TripleForm({'left': '1', 'middle': '2', 'right': '3'})
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(form.is_valid())
|
2009-04-18 23:51:11 +08:00
|
|
|
|
|
|
|
form = TripleForm({'left': '1', 'middle': '3', 'right': '1'})
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(form.is_valid())
|
2009-04-18 23:51:11 +08:00
|
|
|
|
2010-01-21 10:28:03 +08:00
|
|
|
class TripleFormWithCleanOverride(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Triple
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if not self.cleaned_data['left'] == self.cleaned_data['right']:
|
|
|
|
raise forms.ValidationError('Left and right should be equal')
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
class OverrideCleanTests(TestCase):
|
|
|
|
def test_override_clean(self):
|
|
|
|
"""
|
|
|
|
Regression for #12596: Calling super from ModelForm.clean() should be
|
|
|
|
optional.
|
|
|
|
"""
|
|
|
|
form = TripleFormWithCleanOverride({'left': 1, 'middle': 2, 'right': 1})
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(form.is_valid())
|
2010-01-21 10:28:03 +08:00
|
|
|
# form.instance.left will be None if the instance was not constructed
|
|
|
|
# by form.full_clean().
|
|
|
|
self.assertEquals(form.instance.left, 1)
|
|
|
|
|
2010-03-07 02:42:56 +08:00
|
|
|
# Regression test for #12960.
|
|
|
|
# Make sure the cleaned_data returned from ModelForm.clean() is applied to the
|
|
|
|
# model instance.
|
|
|
|
|
|
|
|
class PublicationForm(forms.ModelForm):
|
|
|
|
def clean(self):
|
|
|
|
self.cleaned_data['title'] = self.cleaned_data['title'].upper()
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Publication
|
|
|
|
|
|
|
|
class ModelFormCleanTest(TestCase):
|
|
|
|
def test_model_form_clean_applies_to_model(self):
|
|
|
|
data = {'title': 'test', 'date_published': '2010-2-25'}
|
|
|
|
form = PublicationForm(data)
|
|
|
|
publication = form.save()
|
|
|
|
self.assertEqual(publication.title, 'TEST')
|
|
|
|
|
2009-04-18 23:51:11 +08:00
|
|
|
class FPForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = FilePathModel
|
|
|
|
|
|
|
|
class FilePathFieldTests(TestCase):
|
|
|
|
def test_file_path_field_blank(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8842: FilePathField(blank=True)
|
|
|
|
"""
|
|
|
|
form = FPForm()
|
|
|
|
names = [p[1] for p in form['path'].field.choices]
|
|
|
|
names.sort()
|
|
|
|
self.assertEqual(names, ['---------', '__init__.py', 'models.py', 'tests.py'])
|
2009-05-02 15:03:33 +08:00
|
|
|
|
|
|
|
class ManyToManyCallableInitialTests(TestCase):
|
|
|
|
def test_callable(self):
|
|
|
|
"Regression for #10349: A callable can be provided as the initial value for an m2m field"
|
|
|
|
|
|
|
|
# Set up a callable initial value
|
|
|
|
def formfield_for_dbfield(db_field, **kwargs):
|
|
|
|
if db_field.name == 'publications':
|
2009-05-07 22:48:04 +08:00
|
|
|
kwargs['initial'] = lambda: Publication.objects.all().order_by('date_published')[:2]
|
2009-05-02 15:03:33 +08:00
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
|
|
|
|
# Set up some Publications to use as data
|
2009-05-07 22:48:04 +08:00
|
|
|
Publication(title="First Book", date_published=date(2007,1,1)).save()
|
|
|
|
Publication(title="Second Book", date_published=date(2008,1,1)).save()
|
|
|
|
Publication(title="Third Book", date_published=date(2009,1,1)).save()
|
2009-05-02 15:03:33 +08:00
|
|
|
|
|
|
|
# Create a ModelForm, instantiate it, and check that the output is as expected
|
|
|
|
ModelForm = modelform_factory(Article, formfield_callback=formfield_for_dbfield)
|
|
|
|
form = ModelForm()
|
|
|
|
self.assertEquals(form.as_ul(), u"""<li><label for="id_headline">Headline:</label> <input id="id_headline" type="text" name="headline" maxlength="100" /></li>
|
|
|
|
<li><label for="id_publications">Publications:</label> <select multiple="multiple" name="publications" id="id_publications">
|
|
|
|
<option value="1" selected="selected">First Book</option>
|
|
|
|
<option value="2" selected="selected">Second Book</option>
|
|
|
|
<option value="3">Third Book</option>
|
2010-08-07 01:47:58 +08:00
|
|
|
</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>""")
|
2009-05-20 07:13:33 +08:00
|
|
|
|
|
|
|
class CFFForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = CustomFF
|
|
|
|
|
|
|
|
class CustomFieldSaveTests(TestCase):
|
|
|
|
def test_save(self):
|
|
|
|
"Regression for #11149: save_form_data should be called only once"
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2009-05-20 07:13:33 +08:00
|
|
|
# It's enough that the form saves without error -- the custom save routine will
|
|
|
|
# generate an AssertionError if it is called more than once during save.
|
|
|
|
form = CFFForm(data = {'f': None})
|
2009-12-14 01:46:52 +08:00
|
|
|
form.save()
|
|
|
|
|
|
|
|
class ModelChoiceIteratorTests(TestCase):
|
|
|
|
def test_len(self):
|
|
|
|
class Form(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Article
|
|
|
|
fields = ["publications"]
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2009-12-14 01:46:52 +08:00
|
|
|
Publication.objects.create(title="Pravda",
|
|
|
|
date_published=date(1991, 8, 22))
|
|
|
|
f = Form()
|
|
|
|
self.assertEqual(len(f.fields["publications"].choices), 1)
|
2010-02-10 08:34:45 +08:00
|
|
|
|
|
|
|
class RealPersonForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = RealPerson
|
|
|
|
|
|
|
|
class CustomModelFormSaveMethod(TestCase):
|
|
|
|
def test_string_message(self):
|
|
|
|
data = {'name': 'anonymous'}
|
|
|
|
form = RealPersonForm(data)
|
|
|
|
self.assertEqual(form.is_valid(), False)
|
2010-02-23 22:59:30 +08:00
|
|
|
self.assertEqual(form.errors['__all__'], ['Please specify a real name.'])
|
2010-02-10 08:34:45 +08:00
|
|
|
|
2010-02-23 22:59:30 +08:00
|
|
|
class ModelClassTests(TestCase):
|
|
|
|
def test_no_model_class(self):
|
|
|
|
class NoModelModelForm(forms.ModelForm):
|
|
|
|
pass
|
|
|
|
self.assertRaises(ValueError, NoModelModelForm)
|
2010-02-24 04:02:18 +08:00
|
|
|
|
|
|
|
class OneToOneFieldTests(TestCase):
|
|
|
|
def test_assignment_of_none(self):
|
|
|
|
class AuthorForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Author
|
|
|
|
fields = ['publication', 'full_name']
|
|
|
|
|
|
|
|
publication = Publication.objects.create(title="Pravda",
|
|
|
|
date_published=date(1991, 8, 22))
|
|
|
|
author = Author.objects.create(publication=publication, full_name='John Doe')
|
|
|
|
form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
|
|
|
|
self.assert_(form.is_valid())
|
|
|
|
self.assertEqual(form.cleaned_data['publication'], None)
|
|
|
|
author = form.save()
|
|
|
|
# author object returned from form still retains original publication object
|
|
|
|
# that's why we need to retreive it from database again
|
|
|
|
new_author = Author.objects.get(pk=author.pk)
|
|
|
|
self.assertEqual(new_author.publication, None)
|
|
|
|
|
|
|
|
def test_assignment_of_none_null_false(self):
|
|
|
|
class AuthorForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Author1
|
|
|
|
fields = ['publication', 'full_name']
|
|
|
|
|
|
|
|
publication = Publication.objects.create(title="Pravda",
|
|
|
|
date_published=date(1991, 8, 22))
|
|
|
|
author = Author1.objects.create(publication=publication, full_name='John Doe')
|
|
|
|
form = AuthorForm({'publication':u'', 'full_name':'John Doe'}, instance=author)
|
|
|
|
self.assert_(not form.is_valid())
|
|
|
|
|
2010-03-09 07:55:04 +08:00
|
|
|
|
|
|
|
class ModelChoiceForm(forms.Form):
|
|
|
|
person = ModelChoiceField(Person.objects.all())
|
|
|
|
|
|
|
|
|
|
|
|
class TestTicket11183(TestCase):
|
|
|
|
def test_11183(self):
|
|
|
|
form1 = ModelChoiceForm()
|
|
|
|
field1 = form1.fields['person']
|
2010-08-05 11:59:20 +08:00
|
|
|
# To allow the widget to change the queryset of field1.widget.choices correctly,
|
2010-03-09 07:55:04 +08:00
|
|
|
# without affecting other forms, the following must hold:
|
|
|
|
self.assert_(field1 is not ModelChoiceForm.base_fields['person'])
|
|
|
|
self.assert_(field1.widget.choices.field is field1)
|
2010-08-05 11:59:20 +08:00
|
|
|
|
|
|
|
class HomepageForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Homepage
|
|
|
|
|
|
|
|
class URLFieldTests(TestCase):
|
|
|
|
def test_url_on_modelform(self):
|
|
|
|
"Check basic URL field validation on model forms"
|
|
|
|
self.assertFalse(HomepageForm({'url': 'foo'}).is_valid())
|
|
|
|
self.assertFalse(HomepageForm({'url': 'http://'}).is_valid())
|
|
|
|
self.assertFalse(HomepageForm({'url': 'http://example'}).is_valid())
|
|
|
|
self.assertFalse(HomepageForm({'url': 'http://example.'}).is_valid())
|
|
|
|
self.assertFalse(HomepageForm({'url': 'http://com.'}).is_valid())
|
|
|
|
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://localhost'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://example.com'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://www.example.com'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://www.example.com/test'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://www.example.com:8000/test'}).is_valid())
|
|
|
|
self.assertTrue(HomepageForm({'url': 'http://example.com/foo/bar'}).is_valid())
|
|
|
|
|
|
|
|
def test_http_prefixing(self):
|
|
|
|
"If the http:// prefix is omitted on form input, the field adds it again. (Refs #13613)"
|
|
|
|
form = HomepageForm({'url': 'example.com'})
|
|
|
|
form.is_valid()
|
|
|
|
# self.assertTrue(form.is_valid())
|
|
|
|
# self.assertEquals(form.cleaned_data['url'], 'http://example.com/')
|
|
|
|
|
|
|
|
form = HomepageForm({'url': 'example.com/test'})
|
|
|
|
form.is_valid()
|
|
|
|
# self.assertTrue(form.is_valid())
|
|
|
|
# self.assertEquals(form.cleaned_data['url'], 'http://example.com/test')
|
2010-09-11 06:46:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FormFieldCallbackTests(TestCase):
|
|
|
|
|
|
|
|
def test_baseform_with_widgets_in_meta(self):
|
|
|
|
"""Regression for #13095: Using base forms with widgets defined in Meta should not raise errors."""
|
|
|
|
widget = forms.Textarea()
|
|
|
|
|
|
|
|
class BaseForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
widgets = {'name': widget}
|
|
|
|
|
|
|
|
Form = modelform_factory(Person, form=BaseForm)
|
|
|
|
self.assertTrue(Form.base_fields['name'].widget is widget)
|
|
|
|
|
|
|
|
def test_custom_callback(self):
|
|
|
|
"""Test that a custom formfield_callback is used if provided"""
|
|
|
|
|
|
|
|
callback_args = []
|
|
|
|
|
|
|
|
def callback(db_field, **kwargs):
|
|
|
|
callback_args.append((db_field, kwargs))
|
|
|
|
return db_field.formfield(**kwargs)
|
|
|
|
|
|
|
|
widget = forms.Textarea()
|
|
|
|
|
|
|
|
class BaseForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
widgets = {'name': widget}
|
|
|
|
|
|
|
|
_ = modelform_factory(Person, form=BaseForm,
|
|
|
|
formfield_callback=callback)
|
|
|
|
id_field, name_field = Person._meta.fields
|
|
|
|
|
|
|
|
self.assertEqual(callback_args,
|
|
|
|
[(id_field, {}), (name_field, {'widget': widget})])
|
|
|
|
|
|
|
|
def test_bad_callback(self):
|
|
|
|
# A bad callback provided by user still gives an error
|
|
|
|
self.assertRaises(TypeError, modelform_factory, Person,
|
|
|
|
formfield_callback='not a function or callable')
|
|
|
|
|
2010-09-11 09:39:16 +08:00
|
|
|
|
|
|
|
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:
|
2010-10-12 11:33:19 +08:00
|
|
|
# Make sure the exception contains some reference to the
|
2010-09-11 09:39:16 +08:00
|
|
|
# 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'])
|
|
|
|
|
2010-10-01 10:02:58 +08:00
|
|
|
|
|
|
|
class DocumentForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Document
|
|
|
|
|
|
|
|
class FileFieldTests(unittest.TestCase):
|
|
|
|
def test_clean_false(self):
|
|
|
|
"""
|
|
|
|
If the ``clean`` method on a non-required FileField receives False as
|
|
|
|
the data (meaning clear the field value), it returns False, regardless
|
|
|
|
of the value of ``initial``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
f = forms.FileField(required=False)
|
|
|
|
self.assertEqual(f.clean(False), False)
|
|
|
|
self.assertEqual(f.clean(False, 'initial'), False)
|
|
|
|
|
|
|
|
def test_clean_false_required(self):
|
|
|
|
"""
|
|
|
|
If the ``clean`` method on a required FileField receives False as the
|
|
|
|
data, it has the same effect as None: initial is returned if non-empty,
|
|
|
|
otherwise the validation catches the lack of a required value.
|
|
|
|
|
|
|
|
"""
|
|
|
|
f = forms.FileField(required=True)
|
|
|
|
self.assertEqual(f.clean(False, 'initial'), 'initial')
|
|
|
|
self.assertRaises(ValidationError, f.clean, False)
|
|
|
|
|
|
|
|
def test_full_clear(self):
|
|
|
|
"""
|
|
|
|
Integration happy-path test that a model FileField can actually be set
|
|
|
|
and cleared via a ModelForm.
|
|
|
|
|
|
|
|
"""
|
|
|
|
form = DocumentForm()
|
|
|
|
self.assert_('name="myfile"' in unicode(form))
|
|
|
|
self.assert_('myfile-clear' not in unicode(form))
|
|
|
|
form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', 'content')})
|
|
|
|
self.assert_(form.is_valid())
|
|
|
|
doc = form.save(commit=False)
|
|
|
|
self.assertEqual(doc.myfile.name, 'something.txt')
|
|
|
|
form = DocumentForm(instance=doc)
|
|
|
|
self.assert_('myfile-clear' in unicode(form))
|
|
|
|
form = DocumentForm(instance=doc, data={'myfile-clear': 'true'})
|
|
|
|
doc = form.save(commit=False)
|
|
|
|
self.assertEqual(bool(doc.myfile), False)
|
|
|
|
|
|
|
|
def test_clear_and_file_contradiction(self):
|
|
|
|
"""
|
|
|
|
If the user submits a new file upload AND checks the clear checkbox,
|
|
|
|
they get a validation error, and the bound redisplay of the form still
|
|
|
|
includes the current file and the clear checkbox.
|
|
|
|
|
|
|
|
"""
|
|
|
|
form = DocumentForm(files={'myfile': SimpleUploadedFile('something.txt', 'content')})
|
|
|
|
self.assert_(form.is_valid())
|
|
|
|
doc = form.save(commit=False)
|
|
|
|
form = DocumentForm(instance=doc,
|
|
|
|
files={'myfile': SimpleUploadedFile('something.txt', 'content')},
|
|
|
|
data={'myfile-clear': 'true'})
|
|
|
|
self.assert_(not form.is_valid())
|
|
|
|
self.assertEqual(form.errors['myfile'],
|
|
|
|
[u'Please either submit a file or check the clear checkbox, not both.'])
|
|
|
|
rendered = unicode(form)
|
|
|
|
self.assert_('something.txt' in rendered)
|
|
|
|
self.assert_('myfile-clear' in rendered)
|
2010-10-13 12:36:51 +08:00
|
|
|
|
|
|
|
class EditionForm(forms.ModelForm):
|
|
|
|
author = forms.ModelChoiceField(queryset=Person.objects.all())
|
|
|
|
publication = forms.ModelChoiceField(queryset=Publication.objects.all())
|
|
|
|
edition = forms.IntegerField()
|
|
|
|
isbn = forms.CharField(max_length=13)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Edition
|
|
|
|
|
|
|
|
class UniqueErrorsTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.author1 = Person.objects.create(name=u'Author #1')
|
|
|
|
self.author2 = Person.objects.create(name=u'Author #2')
|
|
|
|
self.pub1 = Publication.objects.create(title='Pub #1', date_published=date(2000, 10, 31))
|
|
|
|
self.pub2 = Publication.objects.create(title='Pub #2', date_published=date(2004, 1, 5))
|
|
|
|
form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161484100'})
|
|
|
|
form.save()
|
|
|
|
|
|
|
|
def test_unique_error_message(self):
|
|
|
|
form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub2.pk, 'edition': 1, 'isbn': '9783161484100'})
|
|
|
|
self.assertEquals(form.errors, {'isbn': [u'Edition with this Isbn already exists.']})
|
|
|
|
|
|
|
|
def test_unique_together_error_message(self):
|
|
|
|
form = EditionForm(data={'author': self.author1.pk, 'publication': self.pub1.pk, 'edition': 2, 'isbn': '9783161489999'})
|
|
|
|
self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']})
|
|
|
|
form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'})
|
|
|
|
self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']})
|
2010-10-13 12:46:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
class EmptyFieldsTestCase(TestCase):
|
|
|
|
"Tests for fields=() cases as reported in #14119"
|
|
|
|
class EmptyPersonForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Person
|
|
|
|
fields = ()
|
|
|
|
|
|
|
|
def test_empty_fields_to_fields_for_model(self):
|
|
|
|
"An argument of fields=() to fields_for_model should return an empty dictionary"
|
|
|
|
field_dict = fields_for_model(Person, fields=())
|
|
|
|
self.assertEqual(len(field_dict), 0)
|
|
|
|
|
|
|
|
def test_empty_fields_on_modelform(self):
|
|
|
|
"No fields on a ModelForm should actually result in no fields"
|
|
|
|
form = self.EmptyPersonForm()
|
|
|
|
self.assertEqual(len(form.fields), 0)
|
|
|
|
|
|
|
|
def test_empty_fields_to_construct_instance(self):
|
|
|
|
"No fields should be set on a model instance if construct_instance receives fields=()"
|
|
|
|
form = modelform_factory(Person)({'name': 'John Doe'})
|
|
|
|
self.assertTrue(form.is_valid())
|
|
|
|
instance = construct_instance(form, Person(), fields=())
|
|
|
|
self.assertEqual(instance.name, '')
|