Hi everyone!
I have a Comment model with self-referenced field parent
class Comment(MPTTModel):
""" The base model represents comment object in db. """
user = models.ForeignKey(CustomUser, related_name="comments", on_delete=models.CASCADE)
parent = TreeForeignKey(
"self",
blank=True,
null=True,
related_name="children",
on_delete=models.SET_NULL
)
date_created = models.DateTimeField(
verbose_name="Created at",
auto_now_add=True,
blank=True,
null=True
)
text = models.TextField(max_length=500)
I’ve created few instances of model above without parent (root level comment), but still getting 3 similar queries.
In my QuerySet variable queryset = Comment.objects.filter(level=0).select_related("user", "parent")
seems has no effect since I’m still getting 3 similar queries.
class CommentViewSet(ModelViewSet):
""" Comment Viewset to handle base model operations. """
queryset = Comment.objects.filter(level=0).select_related("user", "parent")
serializer_class = CommentSerializer
The same field for ‘user’ did a trick.
Appreciate any help.