Reputation: 4128
I have following structure with example data:
id season_id title
1 1 Intro
2 1 Second part
3 1 Third part
4 4 Other intro
5 4 Other second part
(don't ask why), where season_id is always point to id of first episode of season...
What i want, to get following:
1 1 Intro
4 4 Other intro
which are first episoded for season, technically speaking - all entries with lowest season_eid
for and i am using following query to get ids of them:
Movie.objects.filter(category_type = 2).values('season_eid').annotate(models.Min('season_eid'))
and having id i can get all data for objects using django orm __in construction.
Can I make grouping / annotate and take all fields/values using only one query? values + annotate gives me only list of dictionaries, but instead of this i would like to get proper objects (lowest value/min of season_eid) with rest of fields.
Upvotes: 1
Views: 348
Reputation: 87235
Movie.objects.annotate(category_min_season = models.Min('category__season_id')).filter(season_id=category_min_season)
assuming that your catgory has FK to the Movies model.
Update:
Actually you don't even need annotation; thanks to the denormalised data you have stored in the table.
You can just do:
Model.objects.filter(id=Q(season_id))
Upvotes: 1