[Django]-Django, how to use 'group by' and 'max' to get complete row in queryset and display related items in template

3๐Ÿ‘

โœ…

I have found the solution for this, new querysets in views.py will be as follows:

id_list = Talk_comment.objects.filter(user=user_info_obj).values('user','talk').annotate(Max('id')).values('id__max')
qs = Talk_comment.objects.filter(pk__in=id_list)
๐Ÿ‘คD Dhaliwal

0๐Ÿ‘

Your views.py code is problematic here. It will return only the values of the user columns with annotations, rather than returning an actual queryset which allows you to walk through the relation between Talk_comment and Talk.

I believe altering that line to the following will get you closer to your goal.

qs = Talk_comment.objects.annotate(max_id=Max('id')).order_by('-max_id')
๐Ÿ‘คWalter

Leave a comment