Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching in inheritance accessor functions #510

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions polymorphic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,15 @@ def __init__(self, *args, **kwargs):
return
self.__class__.polymorphic_super_sub_accessors_replaced = True

def create_accessor_function_for_model(model, accessor_name):
def create_accessor_function_for_model(model, field):
def accessor_function(self):
objects = getattr(model, "_base_objects", model.objects)
attr = objects.get(pk=self.pk)
return attr
try:
rel_obj = field.get_cached_value(self)
except KeyError:
objects = getattr(model, "_base_objects", model.objects)
rel_obj = objects.get(pk=self.pk)
field.set_cached_value(self, rel_obj)
return rel_obj

return accessor_function

Expand All @@ -214,10 +218,14 @@ def accessor_function(self):
type(orig_accessor),
(ReverseOneToOneDescriptor, ForwardManyToOneDescriptor),
):

field = orig_accessor.related \
if isinstance(orig_accessor, ReverseOneToOneDescriptor) else orig_accessor.field

setattr(
self.__class__,
name,
property(create_accessor_function_for_model(model, name)),
property(create_accessor_function_for_model(model, field)),
)

def _get_inheritance_relation_fields_and_models(self):
Expand Down
23 changes: 23 additions & 0 deletions polymorphic/tests/test_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,29 @@ def test_parent_link_and_related_name(self):
# test that we can delete the object
t.delete()

def test_polymorphic__accessor_caching(self):
blog_a = BlogA.objects.create(name="blog")

blog_base = BlogBase.objects.non_polymorphic().get(id=blog_a.id)
blog_a = BlogA.objects.get(id=blog_a.id)

# test reverse accessor & check that we get back cached object on repeated access
self.assertEqual(blog_base.bloga, blog_a)
self.assertIs(blog_base.bloga, blog_base.bloga)
cached_blog_a = blog_base.bloga

# test forward accessor & check that we get back cached object on repeated access
self.assertEqual(blog_a.blogbase_ptr, blog_base)
self.assertIs(blog_a.blogbase_ptr, blog_a.blogbase_ptr)
cached_blog_base = blog_a.blogbase_ptr

# check that refresh_from_db correctly clears cached related objects
blog_base.refresh_from_db()
blog_a.refresh_from_db()

self.assertIsNot(cached_blog_a, blog_base.bloga)
self.assertIsNot(cached_blog_base, blog_a.blogbase_ptr)

def test_polymorphic__aggregate(self):
"""test ModelX___field syntax on aggregate (should work for annotate either)"""

Expand Down