Skip to content

Conversation

mldwg
Copy link

@mldwg mldwg commented Feb 23, 2022

The accessor functions for inheritance relations are overwritten in PolymorphicModel. This also disables the caching usually provided by django, creating multiple redundant SQL queries in certain situations:

parent = Parent.object.get(id=1)
c1 = parent.child  # retrieves child from db
c2 = parent.child  # retrieves child from db again!

This contradicts the default django behavior and can cause unnecessary overhead.

This PR fixes that issue by using the caching mechanism of the underlying field.
The new unit test test_polymorphic__accessor_caching validates this new feature.

@mldwg mldwg changed the title Caching in ingeritance accessor functions Caching in inheritance accessor functions Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant