mirror of https://github.com/django/django.git
Fixed #26784 -- Made ForeignKey.validate() pass `model` to router if model_instance=None.
This commit is contained in:
parent
03f6d272ab
commit
2224a56631
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue