summaryrefslogtreecommitdiff
path: root/tests/modeltests
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-09-01 19:08:08 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-09-01 19:08:08 +0000
commitea05e61b2b8c0c9e5e10faa3f738b09b5f59adf1 (patch)
tree2d6d06282a7f9bd1203a1e7b002e977b1903cc0a /tests/modeltests
parent5f31e9bd33269484689d5d5f078a979f71edcade (diff)
downloaddjango-ea05e61b2b8c0c9e5e10faa3f738b09b5f59adf1.tar.gz
Fixed #8209: `ModelForm`s now validate unique constraints. Alex Gaynor did much of this work, and Brian Rosner helped as well.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8805 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/modeltests')
-rw-r--r--tests/modeltests/model_forms/models.py53
-rw-r--r--tests/modeltests/model_formsets/models.py68
2 files changed, 120 insertions, 1 deletions
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index 5f714fbedb..99680ff34c 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -117,9 +117,26 @@ class CommaSeparatedInteger(models.Model):
def __unicode__(self):
return self.field
+class Product(models.Model):
+ slug = models.SlugField(unique=True)
+
+ def __unicode__(self):
+ return self.slug
+
+class Price(models.Model):
+ price = models.DecimalField(max_digits=10, decimal_places=2)
+ quantity = models.PositiveIntegerField()
+
+ def __unicode__(self):
+ return u"%s for %s" % (self.quantity, self.price)
+
+ class Meta:
+ unique_together = (('price', 'quantity'),)
+
class ArticleStatus(models.Model):
status = models.CharField(max_length=2, choices=ARTICLE_STATUS_CHAR, blank=True, null=True)
+
__test__ = {'API_TESTS': """
>>> from django import forms
>>> from django.forms.models import ModelForm, model_to_dict
@@ -1132,8 +1149,42 @@ u'1,,2'
>>> f.clean('1')
u'1'
-# Choices on CharField and IntegerField
+# unique/unique_together validation
+>>> class ProductForm(ModelForm):
+... class Meta:
+... model = Product
+>>> form = ProductForm({'slug': 'teddy-bear-blue'})
+>>> form.is_valid()
+True
+>>> obj = form.save()
+>>> obj
+<Product: teddy-bear-blue>
+>>> form = ProductForm({'slug': 'teddy-bear-blue'})
+>>> form.is_valid()
+False
+>>> form._errors
+{'slug': [u'Product with this Slug already exists.']}
+>>> form = ProductForm({'slug': 'teddy-bear-blue'}, instance=obj)
+>>> form.is_valid()
+True
+
+# ModelForm test of unique_together constraint
+>>> class PriceForm(ModelForm):
+... class Meta:
+... model = Price
+>>> form = PriceForm({'price': '6.00', 'quantity': '1'})
+>>> form.is_valid()
+True
+>>> form.save()
+<Price: 1 for 6.00>
+>>> form = PriceForm({'price': '6.00', 'quantity': '1'})
+>>> form.is_valid()
+False
+>>> form._errors
+{'__all__': [u'Price with this Price and Quantity already exists.']}
+
+# Choices on CharField and IntegerField
>>> class ArticleForm(ModelForm):
... class Meta:
... model = Article
diff --git a/tests/modeltests/model_formsets/models.py b/tests/modeltests/model_formsets/models.py
index 332c5a7235..1e25baa3a7 100644
--- a/tests/modeltests/model_formsets/models.py
+++ b/tests/modeltests/model_formsets/models.py
@@ -73,6 +73,22 @@ class Restaurant(Place):
def __unicode__(self):
return self.name
+class Product(models.Model):
+ slug = models.SlugField(unique=True)
+
+ def __unicode__(self):
+ return self.slug
+
+class Price(models.Model):
+ price = models.DecimalField(max_digits=10, decimal_places=2)
+ quantity = models.PositiveIntegerField()
+
+ def __unicode__(self):
+ return u"%s for %s" % (self.quantity, self.price)
+
+ class Meta:
+ unique_together = (('price', 'quantity'),)
+
class MexicanRestaurant(Restaurant):
serves_tacos = models.BooleanField()
@@ -553,4 +569,56 @@ True
>>> type(_get_foreign_key(MexicanRestaurant, Owner))
<class 'django.db.models.fields.related.ForeignKey'>
+# unique/unique_together validation ###########################################
+
+>>> FormSet = modelformset_factory(Product, extra=1)
+>>> data = {
+... 'form-TOTAL_FORMS': '1',
+... 'form-INITIAL_FORMS': '0',
+... 'form-0-slug': 'car-red',
+... }
+>>> formset = FormSet(data)
+>>> formset.is_valid()
+True
+>>> formset.save()
+[<Product: car-red>]
+
+>>> data = {
+... 'form-TOTAL_FORMS': '1',
+... 'form-INITIAL_FORMS': '0',
+... 'form-0-slug': 'car-red',
+... }
+>>> formset = FormSet(data)
+>>> formset.is_valid()
+False
+>>> formset.errors
+[{'slug': [u'Product with this Slug already exists.']}]
+
+# unique_together
+
+>>> FormSet = modelformset_factory(Price, extra=1)
+>>> data = {
+... 'form-TOTAL_FORMS': '1',
+... 'form-INITIAL_FORMS': '0',
+... 'form-0-price': u'12.00',
+... 'form-0-quantity': '1',
+... }
+>>> formset = FormSet(data)
+>>> formset.is_valid()
+True
+>>> formset.save()
+[<Price: 1 for 12.00>]
+
+>>> data = {
+... 'form-TOTAL_FORMS': '1',
+... 'form-INITIAL_FORMS': '0',
+... 'form-0-price': u'12.00',
+... 'form-0-quantity': '1',
+... }
+>>> formset = FormSet(data)
+>>> formset.is_valid()
+False
+>>> formset.errors
+[{'__all__': [u'Price with this Price and Quantity already exists.']}]
+
"""}