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

OrderedManyToMany does not respect Django query cache #318

Open
Archmonger opened this issue Feb 10, 2024 · 0 comments
Open

OrderedManyToMany does not respect Django query cache #318

Archmonger opened this issue Feb 10, 2024 · 0 comments

Comments

@Archmonger
Copy link

Archmonger commented Feb 10, 2024

Original discussion: #277 (comment)


By default, Django query instances cache their results so that subsequent calls to the same object do not hit the database. This is a behavior we rely on within ReactPy-Django to maintain ORM compatibility.

However, OrderedManyToMany does not seem to respect that same caching behavior as Django's built-in model fields. For example...

from django.db.models import prefetch_related_objects

categories = SpotlightCategory.objects.all()

# Initial run forces DB query execution to allow results to be stored in cache
for category in categories:
   # This should label the 'apps' field as a value that should be cached
   prefetch_related_objects([category], 'apps')

   print(category.name)  # Call attribute to force `Model` to execute DB queries
   print(category.apps.get_queryset().all())  # Call the `QuerySet` to execute DB queries

# Second run, everything should already be stored in cache
for category in categories:
   print(category.name)  # This works as expected (the result is cached)
   print(category.apps.get_queryset().all())  # This causes a new query to be performed. It should have been cached.
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

No branches or pull requests

1 participant