G
G
Gennady Karev2015-01-31 22:03:13
Django
Gennady Karev, 2015-01-31 22:03:13

How to get date sorted distinct by foreignkey?

In [102]: Episode.objects.order_by('-date_modified').filter(series__series_type='series').values_list('series_id', flat=True).distinct()
Out[102]: [71L, 181L, 181L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 411L, 401L, 401L, 401L, '...(remaining elements truncated)...']

Where series_id foreign key.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gennady Karev, 2015-02-02
@Warlegend

This is how I got a list of unique ids sorted by date.

list(Episode.objects.values_list(
                'series_id', flat=True
            ).filter(
                series__series_type='series'
            ).annotate(
                Max("date_modified")
            ).order_by(
                '-date_modified__max'
            ))

A
Andrey K, 2015-02-01
@mututunus

https://docs.djangoproject.com/en/1.7/ref/models/q...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question