Aashay Amballi
Aashay Amballi

Reputation: 1411

ORM to the distinct values from a model based on the latest datetime field in Django

Consider I have a model named History and it has data like this.

name       comment           date
----------------------------------
Jake    some comment     2017-06-20
John    some comment     2017-08-20
Jake    some comment     2017-06-21
Albert  some comment     2017-06-21

and from the above sample data, I want to get distinct data based on the datetime field. For example, the result should look like below from the above sample data.

name       comment           date
----------------------------------
John    some comment     2017-08-20
Jake    some comment     2017-06-21
Albert  some comment     2017-06-21

How can I write an ORM for this? can anyone please help me!

Upvotes: 0

Views: 24

Answers (1)

Davit Tovmasyan
Davit Tovmasyan

Reputation: 3588

I think you should try this:

History.objects.order_by('name', '-date').distinct('name')

Upvotes: 2

Related Questions