Reputation: 341
I have a model Sales with:
I would like to get the first sale for each date and for each saler, how can I do that? Thanks
Upvotes: 3
Views: 1669
Reputation: 1832
Not tested, but I would try (I assumed the field for the date of the sale is named sale_date
, and is of type Datetime):
first_sale = Sales.objects.filter(saler=the_saler, sale_date__date=datetime.date(2021, 05, 19)).order_by('sale_date').first()
filter
will restrict the search to a given saler (the_saler
), and to a given day (see the __date
expression: https://docs.djangoproject.com/fr/3.1/ref/models/querysets/#date)order_by
and first
will give you the first of the day.Upvotes: 1
Reputation: 66
Considering the model Posted in the question, the Django ORM query will be:
first_sale = Sales.objects.order_by("Saler", "Date").distinct("Saler")
Upvotes: 2