[1.2.X] Fixed #14691 -- Made ForeignKey.validate() use the right database. Thanks Marco Paolini for the report.

Backport of [14580] from trunk

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14581 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Ramiro Morales 2010-11-16 22:17:51 +00:00
parent 6ff1f38646
commit 746ce7d967
2 changed files with 10 additions and 1 deletions

View File

@ -829,7 +829,10 @@ class ForeignKey(RelatedField, Field):
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 File

@ -580,6 +580,12 @@ class QueryTestCase(TestCase):
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