I have 2 models, Form and Row, Each Row has a FK to a Form. I want to sort my Forms by the time of last row addition.
class Form(models.Model):
...
class Row(models.Model):
form = models.ForeignKey(
Form,
related_name="rows",
)
created_at = models.DateTimeField(
auto_now_add=True,
)
I tried this but it doesnt work:
queryset = Form.objects.filter(is_empty=False)
queryset = queryset.annotate(last_submit=Max("rows__created_at")).order_by('-last_submit')