[Fixed]-Getting most recent by joining subquery in Django ORM

1👍

I suppose this should do

    Table.objects.order_by('-user_id').distinct('user_id')

See distinct() in this -> https://docs.djangoproject.com/en/1.9/ref/models/querysets/

But this will only work if the latest log entry for that user is the last entry by that user in the table, i.e., the log entries of a particular user as sorted in ascending way in the table.

0👍

You may try:

 User.objects.latest().id

0👍

Table.objects.order_by('user_id', '-datetime').distinct('user_id')

Add indexes to user_id and datetime (Meta.index_together = ['user_id', 'datetime']).

Leave a comment