summaryrefslogtreecommitdiff
path: root/tests/model_forms
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2013-06-07 11:15:34 +0100
committerAndrew Godwin <andrew@aeracode.org>2013-06-07 11:15:34 +0100
commit3c296382b8dea5de7f4e1e11b66bd7cecaf2ee51 (patch)
tree0ca12593be82971691ffca01a836d00d3fcb3bd4 /tests/model_forms
parent7609e0b42e0014a6ad0adf9dafc7018cb268070e (diff)
parent357d62d9f2972bf1bc21e5835c12c849143e06af (diff)
Merge remote-tracking branch 'core/master' into schema-alteration
Conflicts: django/db/models/fields/related.py
Diffstat (limited to 'tests/model_forms')
-rw-r--r--tests/model_forms/models.py16
-rw-r--r--tests/model_forms/tests.py70
2 files changed, 78 insertions, 8 deletions
diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py
index a79d9b8c5b..610dc34001 100644
--- a/tests/model_forms/models.py
+++ b/tests/model_forms/models.py
@@ -207,7 +207,17 @@ class Post(models.Model):
posted = models.DateField()
def __str__(self):
- return self.name
+ return self.title
+
+@python_2_unicode_compatible
+class DateTimePost(models.Model):
+ title = models.CharField(max_length=50, unique_for_date='posted', blank=True)
+ slug = models.CharField(max_length=50, unique_for_year='posted', blank=True)
+ subtitle = models.CharField(max_length=50, unique_for_month='posted', blank=True)
+ posted = models.DateTimeField(editable=False)
+
+ def __str__(self):
+ return self.title
class DerivedPost(Post):
pass
@@ -255,3 +265,7 @@ class Colour(models.Model):
class ColourfulItem(models.Model):
name = models.CharField(max_length=50)
colours = models.ManyToManyField(Colour)
+
+class ArticleStatusNote(models.Model):
+ name = models.CharField(max_length=20)
+ status = models.ManyToManyField(ArticleStatus)
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index c5db011404..58dde13a8a 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -24,7 +24,7 @@ from .models import (Article, ArticleStatus, BetterAuthor, BigInt,
DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
ImprovedArticleWithParentLink, Inventory, Post, Price,
Product, TextFile, AuthorProfile, Colour, ColourfulItem,
- test_images)
+ ArticleStatusNote, DateTimePost, test_images)
if test_images:
from .models import ImageFile, OptionalImageFile
@@ -76,6 +76,12 @@ class PostForm(forms.ModelForm):
fields = '__all__'
+class DateTimePostForm(forms.ModelForm):
+ class Meta:
+ model = DateTimePost
+ fields = '__all__'
+
+
class DerivedPostForm(forms.ModelForm):
class Meta:
model = DerivedPost
@@ -234,6 +240,20 @@ class ColourfulItemForm(forms.ModelForm):
model = ColourfulItem
fields = '__all__'
+# model forms for testing work on #9321:
+
+class StatusNoteForm(forms.ModelForm):
+ class Meta:
+ model = ArticleStatusNote
+ fields = '__all__'
+
+
+class StatusNoteCBM2mForm(forms.ModelForm):
+ class Meta:
+ model = ArticleStatusNote
+ fields = '__all__'
+ widgets = {'status': forms.CheckboxSelectMultiple}
+
class ModelFormBaseTest(TestCase):
def test_base_form(self):
@@ -274,8 +294,8 @@ class ModelFormBaseTest(TestCase):
model = Category
fields = '__all__'
- self.assertTrue(isinstance(ReplaceField.base_fields['url'],
- forms.fields.BooleanField))
+ self.assertIsInstance(ReplaceField.base_fields['url'],
+ forms.fields.BooleanField)
def test_replace_field_variant_2(self):
# Should have the same result as before,
@@ -287,8 +307,8 @@ class ModelFormBaseTest(TestCase):
model = Category
fields = ['url']
- self.assertTrue(isinstance(ReplaceField.base_fields['url'],
- forms.fields.BooleanField))
+ self.assertIsInstance(ReplaceField.base_fields['url'],
+ forms.fields.BooleanField)
def test_replace_field_variant_3(self):
# Should have the same result as before,
@@ -300,8 +320,8 @@ class ModelFormBaseTest(TestCase):
model = Category
fields = [] # url will still appear, since it is explicit above
- self.assertTrue(isinstance(ReplaceField.base_fields['url'],
- forms.fields.BooleanField))
+ self.assertIsInstance(ReplaceField.base_fields['url'],
+ forms.fields.BooleanField)
def test_override_field(self):
class AuthorForm(forms.ModelForm):
@@ -668,6 +688,23 @@ class UniqueTest(TestCase):
self.assertEqual(len(form.errors), 1)
self.assertEqual(form.errors['posted'], ['This field is required.'])
+ def test_unique_for_date_in_exclude(self):
+ """If the date for unique_for_* constraints is excluded from the
+ ModelForm (in this case 'posted' has editable=False, then the
+ constraint should be ignored."""
+ p = DateTimePost.objects.create(title="Django 1.0 is released",
+ slug="Django 1.0", subtitle="Finally",
+ posted=datetime.datetime(2008, 9, 3, 10, 10, 1))
+ # 'title' has unique_for_date='posted'
+ form = DateTimePostForm({'title': "Django 1.0 is released", 'posted': '2008-09-03'})
+ self.assertTrue(form.is_valid())
+ # 'slug' has unique_for_year='posted'
+ form = DateTimePostForm({'slug': "Django 1.0", 'posted': '2008-01-01'})
+ self.assertTrue(form.is_valid())
+ # 'subtitle' has unique_for_month='posted'
+ form = DateTimePostForm({'subtitle': "Finally", 'posted': '2008-09-30'})
+ self.assertTrue(form.is_valid())
+
def test_inherited_unique_for_date(self):
p = Post.objects.create(title="Django 1.0 is released",
slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
@@ -1677,3 +1714,22 @@ class OldFormForXTests(TestCase):
<option value="%(blue_pk)s">Blue</option>
</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>"""
% {'blue_pk': colour.pk})
+
+
+class M2mHelpTextTest(TestCase):
+ """Tests for ticket #9321."""
+ def test_multiple_widgets(self):
+ """Help text of different widgets for ManyToManyFields model fields"""
+ dreaded_help_text = '<span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span>'
+
+ # Default widget (SelectMultiple):
+ std_form = StatusNoteForm()
+ self.assertInHTML(dreaded_help_text, std_form.as_p())
+
+ # Overridden widget (CheckboxSelectMultiple, a subclass of
+ # SelectMultiple but with a UI that doesn't involve Control/Command
+ # keystrokes to extend selection):
+ form = StatusNoteCBM2mForm()
+ html = form.as_p()
+ self.assertInHTML('<ul id="id_status">', html)
+ self.assertInHTML(dreaded_help_text, html, count=0)