Skip to content

Instantly share code, notes, and snippets.

@apinsard
Last active October 11, 2022 12:17
Show Gist options
  • Save apinsard/9aafbcc3c4bb24d4732c6ed0f6c9bc36 to your computer and use it in GitHub Desktop.
Save apinsard/9aafbcc3c4bb24d4732c6ed0f6c9bc36 to your computer and use it in GitHub Desktop.
Example of QuerySet._result_cache not set when calling QuerySet.__repr__
>>> a = Tag.objects.filter(pk=1)
>>> a._result_cache
>>> a
<QuerySet [<Tag: Test>]>
>>> a._result_cache
>>> for b in a:
... pass
...
>>> a._result_cache
[<Tag: Test>]
>>>
>>> from django.utils.functional import cached_property
>>> class X:
... @cached_property
... def tags(self):
... return Tag.objects.filter(pk=1)
...
>>> x = X()
>>> x.tags._result_cache
>>> x.tags
<QuerySet [<Tag: Test>]>
>>> x.tags._result_cache
>>> for y in x.tags:
... pass
...
>>> x.tags._result_cache
[<Tag: Test>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment