I’d like to obtain a QuerySet
containing all the latest comments for each post using Django ORM. The comments can be ordered ordered by the when
field.
Suppose the following example:
class Post(models.Model):
name = models.TextField()
class Comment(models.Model):
title = models.TextField()
when = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(Post, on_delete=models.CASCADE)
This in raw SQL could be achieved, so I assume Django may have its own way too. I’d like to avoid using raw queries (discouraged by Django documentation).
I cannot use distinct()
with fields names as I am using MySQL.
Any indication is appreciated.