Reputation: 3
I have a model:
class Item(models.Model):
date = models.DateField()
I would like to select one of these objects for each date, with no duplicates.
So if there were 100 items in the database, which had dates of either 1/1/12 or 1/2/12, I would want to return a list of two objects (one for 1/1/12 and one for 1/2/12).
I'm not sure of the terminology for this kind of query, so am having trouble searching for an answer.
I'm currently using this query:
item_list = Item.objects.distinct('date')
But it is not working as I expected.
Any help appriciated.
Thanks for reading.
Upvotes: 0
Views: 2938
Reputation: 53316
Are you using Postgress SQL? Django documentation says distinct on fields works only with that DB. Also you have to use order_by
before using distinct()
.
Check documentation : django distinct
Upvotes: 6