diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py
index db08329..a2fd24c 100644
a
|
b
|
def validate_answer_to_universe(value):
|
11 | 11 | if value != 42: |
12 | 12 | raise ValidationError('This is not the answer to life, universe and everything!', code='not42') |
13 | 13 | |
| 14 | def validate_m2m_fied(value): |
| 15 | raise ValidationError('Many to many validator called') |
| 16 | |
| 17 | |
14 | 18 | class ModelToValidate(models.Model): |
15 | 19 | name = models.CharField(max_length=100) |
16 | 20 | created = models.DateTimeField(default=datetime.now) |
… |
… |
try:
|
108 | 112 | except AssertionError as exc: |
109 | 113 | assertion_error = exc |
110 | 114 | assert str(assertion_error) == "A model can't have more than one AutoField." |
| 115 | |
| 116 | |
| 117 | class M2mModel(models.Model): |
| 118 | authors = models.ManyToManyField(Author, validators=[validate_m2m_fied]) |
diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py
index 58b7b94..65678b9 100644
a
|
b
|
from django.test import TestCase
|
6 | 6 | |
7 | 7 | from . import ValidationTestCase |
8 | 8 | from .models import (Author, Article, ModelToValidate, |
9 | | GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest) |
| 9 | GenericIPAddressTestModel, GenericIPAddrUnpackUniqueTest, M2mModel) |
10 | 10 | |
11 | 11 | # Import other tests for this package. |
12 | 12 | from .test_custom_messages import CustomMessagesTest |
… |
… |
class ArticleForm(forms.ModelForm):
|
67 | 67 | model = Article |
68 | 68 | exclude = ['author'] |
69 | 69 | |
| 70 | |
| 71 | class M2mForm(forms.ModelForm): |
| 72 | class Meta: |
| 73 | model = M2mModel |
| 74 | |
| 75 | |
70 | 76 | class ModelFormsTests(TestCase): |
71 | 77 | def setUp(self): |
72 | 78 | self.author = Author.objects.create(name='Joseph Kocherhans') |
… |
… |
class ModelFormsTests(TestCase):
|
110 | 116 | form = ArticleForm(data, instance=article) |
111 | 117 | self.assertEqual(list(form.errors), ['pub_date']) |
112 | 118 | |
| 119 | def test_m2m_validator_gets_called(self): |
| 120 | # checks if the custom validator on the m2m field authors gets called |
| 121 | form = M2mForm({'authors': [self.author.pk]}) |
| 122 | self.assertEqual(list(form.errors), ['authors']) |
| 123 | |
113 | 124 | |
114 | 125 | class GenericIPAddressFieldTests(ValidationTestCase): |
115 | 126 | |