From acc095c333213eb1d978866ae5852cefe00ce4ef Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Thu, 21 Jan 2010 03:26:14 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#12582.=20Model=20validation=20on=20For?= =?UTF-8?q?eignKeys=20now=20respects=20limit=5Fchoices=5Fto.=20Thanks,=20H?= =?UTF-8?q?onza=20Kr=C3=A1l.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@12271 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 11 ++++++----- tests/modeltests/validation/models.py | 2 +- tests/modeltests/validation/tests.py | 9 ++++++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index e3d4a18264..36dde5b7f4 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -770,11 +770,12 @@ class ForeignKey(RelatedField, Field): super(ForeignKey, self).validate(value, model_instance) if not value: return - try: - self.rel.to._default_manager.get(**{self.rel.field_name:value}) - except self.rel.to.DoesNotExist, e: - raise exceptions.ValidationError( - self.error_messages['invalid'] % {'model': self.rel.to._meta.verbose_name, 'pk': value}) + + qs = self.rel.to._default_manager.filter(**{self.rel.field_name:value}) + qs = qs.complex_filter(self.rel.limit_choices_to) + if not qs.exists(): + raise exceptions.ValidationError(self.error_messages['invalid'] % { + 'model': self.rel.to._meta.verbose_name, 'pk': value}) def get_attname(self): return '%s_id' % self.name diff --git a/tests/modeltests/validation/models.py b/tests/modeltests/validation/models.py index 017373c52a..43a5f5f935 100644 --- a/tests/modeltests/validation/models.py +++ b/tests/modeltests/validation/models.py @@ -12,7 +12,7 @@ class ModelToValidate(models.Model): name = models.CharField(max_length=100) created = models.DateTimeField(default=datetime.now) number = models.IntegerField() - parent = models.ForeignKey('self', blank=True, null=True) + parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'number': 10}) email = models.EmailField(blank=True) url = models.URLField(blank=True) f_with_custom_validator = models.IntegerField(blank=True, null=True, validators=[validate_answer_to_universe]) diff --git a/tests/modeltests/validation/tests.py b/tests/modeltests/validation/tests.py index d7fc8172da..00273931c7 100644 --- a/tests/modeltests/validation/tests.py +++ b/tests/modeltests/validation/tests.py @@ -30,9 +30,16 @@ class BaseModelValidationTests(ValidationTestCase): def test_correct_FK_value_validates(self): parent = ModelToValidate.objects.create(number=10, name='Some Name') - mtv=ModelToValidate(number=10, name='Some Name', parent_id=parent.pk) + mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk) self.assertEqual(None, mtv.full_clean()) + def test_limitted_FK_raises_error(self): + # The limit_choices_to on the parent field says that a parent object's + # number attribute must be 10, so this should fail validation. + parent = ModelToValidate.objects.create(number=11, name='Other Name') + mtv = ModelToValidate(number=10, name='Some Name', parent_id=parent.pk) + self.assertFailsValidation(mtv.full_clean, ['parent']) + def test_wrong_email_value_raises_error(self): mtv = ModelToValidate(number=10, name='Some Name', email='not-an-email') self.assertFailsValidation(mtv.full_clean, ['email'])