Kaydet (Commit) 2224a566 authored tarafından Ben Demboski's avatar Ben Demboski Kaydeden (comit) Tim Graham

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

üst 03f6d272
......@@ -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}
)
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment