MichaelS
MichaelS

Reputation: 341

django - get first record for each day

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

Answers (2)

scandel
scandel

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()

Upvotes: 1

Abhijeet Anand Shah
Abhijeet Anand Shah

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

Related Questions