Fixed #26784 -- Made ForeignKey.validate() pass `model` to router if model_instance=None.

This commit is contained in:
Ben Demboski 2016-06-20 14:52:14 -07:00 committed by Tim Graham
parent 03f6d272ab
commit 2224a56631
2 changed files with 12 additions and 1 deletions

View File

@ -926,7 +926,7 @@ class ForeignKey(ForeignObject):
if value is None:
return
using = router.db_for_read(model_instance.__class__, instance=model_instance)
using = router.db_for_read(self.remote_field.model, instance=model_instance)
qs = self.remote_field.model._default_manager.using(using).filter(
**{self.remote_field.field_name: value}
)

View File

@ -586,6 +586,17 @@ class QueryTestCase(TestCase):
pluto = Pet.objects.using('other').create(name="Pluto", owner=mickey)
self.assertIsNone(pluto.full_clean())
# Any router that accesses `model` in db_for_read() works here.
@override_settings(DATABASE_ROUTERS=[AuthRouter()])
def test_foreign_key_validation_with_router(self):
"""
ForeignKey.validate() passes `model` to db_for_read() even if
model_instance=None.
"""
mickey = Person.objects.create(name="Mickey")
owner_field = Pet._meta.get_field('owner')
self.assertEqual(owner_field.clean(mickey.pk, None), mickey.pk)
def test_o2o_separation(self):
"OneToOne fields are constrained to a single database"
# Create a user and profile on the default database