I am creating a site where the database model looks similar to this.
class Category(models.Model):
name = modles.CharField(max_length=30)
class Photos(models.Model):
name = models.CharField(max_length=30)
category = models.ForeignKey(Category)
Now I am selecting an element from photos and storing it into cache with cache.set('object',object,timeout). Now I try to access photos.name from this cache, no queries are performed. The moment I query for photos.category it performs query. Is there any way to prevent this. I only want the id of the category, after getting the id, I can query the category cache to get the element. What is the solution to implement this. Caching this was has improved my benchmarks significantly, I am trying to get more performance out of it.
If you just want the ID, you can do
photos.category_id.You might also want to explore using
select_related()to get the related category at the time when you query the original photo.