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:
ekaplan1 2023-10-25 11:05:02 -04:00 committed by Brian Kohan
parent 9fcebfed88
commit e9cdaada1b
No known key found for this signature in database
GPG Key ID: 5C6CE8BA38C43FC1
3 changed files with 21 additions and 1 deletions

View File

@ -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>`_

View File

@ -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

View File

@ -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)