mirror of
https://github.com/django-polymorphic/django-polymorphic.git
synced 2025-12-08 10:23:54 +03:00
Support multiple databases in inheritance accessors
Co-authored-by: ekaplan1 <eytan.kaplan@nasa.gov> Co-authored-by: bckohan <bckohan@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
parent
9fcebfed88
commit
e9cdaada1b
|
|
@ -4,6 +4,7 @@ Changelog
|
||||||
v4.3.0 (202X-XX-XX)
|
v4.3.0 (202X-XX-XX)
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
|
* Fixed `"multi-database support in inheritance accessors. <https://github.com/jazzband/django-polymorphic/pull/550>`_
|
||||||
* Fixed `Caching in inheritance accessor functions <https://github.com/jazzband/django-polymorphic/pull/510>`_
|
* Fixed `Caching in inheritance accessor functions <https://github.com/jazzband/django-polymorphic/pull/510>`_
|
||||||
* Fixed `Foreign key resolves to parent class when using abstract models <https://github.com/jazzband/django-polymorphic/issues/437>`_
|
* Fixed `Foreign key resolves to parent class when using abstract models <https://github.com/jazzband/django-polymorphic/issues/437>`_
|
||||||
* Fixed `Support Q expressions that contain subquery expressions <https://github.com/jazzband/django-polymorphic/pull/572>`_
|
* Fixed `Support Q expressions that contain subquery expressions <https://github.com/jazzband/django-polymorphic/pull/572>`_
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ class PolymorphicModel(models.Model, metaclass=PolymorphicModelBase):
|
||||||
rel_obj = field.get_cached_value(self)
|
rel_obj = field.get_cached_value(self)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
objects = getattr(model, "_base_objects", model.objects)
|
objects = getattr(model, "_base_objects", model.objects)
|
||||||
rel_obj = objects.get(pk=self.pk)
|
rel_obj = objects.using(self._state.db or DEFAULT_DB_ALIAS).get(pk=self.pk)
|
||||||
field.set_cached_value(self, rel_obj)
|
field.set_cached_value(self, rel_obj)
|
||||||
return rel_obj
|
return rel_obj
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,9 @@ from polymorphic.tests.models import (
|
||||||
ModelY,
|
ModelY,
|
||||||
One2OneRelatingModel,
|
One2OneRelatingModel,
|
||||||
RelatingModel,
|
RelatingModel,
|
||||||
|
RelationA,
|
||||||
|
RelationB,
|
||||||
|
RelationBase,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -118,3 +121,19 @@ class MultipleDatabasesTests(TestCase):
|
||||||
|
|
||||||
# Ensure no queries are made using the default database.
|
# Ensure no queries are made using the default database.
|
||||||
self.assertNumQueries(0, func)
|
self.assertNumQueries(0, func)
|
||||||
|
|
||||||
|
def test_deletion_cascade_on_non_default_db(self):
|
||||||
|
def run():
|
||||||
|
base_db1 = RelationA.objects.db_manager("secondary").create(field_a="Base DB1")
|
||||||
|
base_db2 = RelationB.objects.db_manager("secondary").create(
|
||||||
|
field_b="Base DB2", fk=base_db1
|
||||||
|
)
|
||||||
|
|
||||||
|
ContentType.objects.clear_cache()
|
||||||
|
|
||||||
|
RelationBase.objects.db_manager("secondary").filter(pk=base_db2.pk).delete()
|
||||||
|
|
||||||
|
self.assertEqual(RelationB.objects.db_manager("secondary").count(), 0)
|
||||||
|
|
||||||
|
# Ensure no queries are made using the default database.
|
||||||
|
self.assertNumQueries(0, run)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user