In the below example, I have questions.
Example
from django.utils.functional import cached_property
class Product(models.Model):
class ProductType(models.TextChoices):
PRODUCT = 'PRODUCT', _('Product')
LISTING = 'LISTING', _('Listing')
my_model = models.ForeignKey(MyModel, on_delete=models.CASCADE, related_name='products')
product_type = models.CharField(max_length=7, choices=ProductType.choices)
class MyModel(models.Model):
...
@cached_property
def listing(self):
return self.products.get(product_type=Product.ProductType.LISTING)
Questions
- Is the
listing
property being cached on theMyModel
object? I ask because it's accessing.get()
of a queryset which has greater implications.
Ex:
instance = MyModel()
instance.listing # hits db
instance.listing # gets from cache?
- How can I set up a scenario to inspect and verify that caching of
instance.listing
is in-fact happening? I read to look in the__dict__
method ofinstance.listing.__dict__
but I don't notice anything specific.