Reputation: 49
The implementation of DocumentDetail's str method works, but is slows everything down by running a bunch of extra queries when I try to use it views, forms, etc. Does anyone know a way around this?
models.py
class Document(models.Model):
description = models.CharField(max_length=50)
def __str__(self):
return self.description
class DocumentDetail(models.Model):
document = models.ForeignKey(Document, on_delete=models.CASCADE)
detail = models.CharField(max_length=50)
def __str__(self):
return self.document.description
Upvotes: 1
Views: 780
Reputation: 476493
You should load the DocumentDetail
with the Document
. You can do this with .select_related(…)
[Django-doc].
Indeed, we can for example load the DocumentDetail
and load the document itself together with the DocumentDetail
s with:
DocumentDetail.objects.select_related('document').get(pk=1)
This will make a LEFT OUTER JOIN
and thus load both the DocumentDetail
and the Document
in the same query.
We can also fetch all DocumentDetail
s with the related Document
s with:
DocumentDetail.objects.select_related('document') # all DocumentDetails
In views/modelviewsets, etc. where you need to call str(…)
on the DocumentDetail
, you thus should rewrite the queries to load the related Document
in the same query.
Upvotes: 2