I have a question. Will these two properties work different and how, I mean db hits.
for example, we have the model Article, that has a ForeignKey field book
from django.db import models
class Article(models.Model):
book = models.ForeignKey("books.Book")
what is the best way to get an author by one of the prorerties:
@property
def author(self):
if self.book:
return self.book.author
return None
or
@property
def author(self):
book = self.book
if book:
return book.author
return None
Since you are defining a property on
Article, the actual database hit depends on howArticlequerysets are retrieved. If you useselect_related([depth=2])on your queryset while retrieving theArticleobjects, that would be the most optimal in terms of database hits irrespective of how you write the property. Both the ways you have listed have similar performance.