Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14691 -- Made ForeignKey.validate() use the right database. Th…

…anks Marco Paolini for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14580 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f3d07047834b0a4ea3bde2b46643b327a659364e 1 parent ea14592
@ramiro ramiro authored
View
5 django/db/models/fields/related.py
@@ -835,7 +835,10 @@ def validate(self, value, model_instance):
if value is None:
return
- qs = self.rel.to._default_manager.filter(**{self.rel.field_name:value})
+ using = router.db_for_read(model_instance.__class__, instance=model_instance)
+ qs = self.rel.to._default_manager.using(using).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'] % {
View
6 tests/regressiontests/multiple_database/tests.py
@@ -581,6 +581,12 @@ def test_foreign_key_deletion(self):
self.assertEquals(Person.objects.using('other').count(), 0)
self.assertEquals(Pet.objects.using('other').count(), 0)
+ def test_foreign_key_validation(self):
+ "ForeignKey.validate() uses the correct database"
+ mickey = Person.objects.using('other').create(name="Mickey")
+ pluto = Pet.objects.using('other').create(name="Pluto", owner=mickey)
+ self.assertEquals(None, pluto.full_clean())
+
def test_o2o_separation(self):
"OneToOne fields are constrained to a single database"
# Create a user and profile on the default database

0 comments on commit f3d0704

Please sign in to comment.
Something went wrong with that request. Please try again.