user6089877
user6089877

Reputation:

Django list ordering by date

I want to display in the same page all the results of this 2 queries and ordering them by date. The goal is to mix both results to just display a unique list ordering by date.

articles = Articles.objects.all()

statut = Statut.objects.all()

I have this idea but I don't know :

articles = list(Articles.objects.all())

statut = list(Statut.objects.all())

all = articles + statut

So I have a unique list and it's working. It displays every results.

Now I wonder how to order by date for the template rendering?

May be there is a simpler way to do it ?

Thank you

Upvotes: 3

Views: 1284

Answers (2)

gzerone
gzerone

Reputation: 2227

Hmm..., my suggestion is sorting them on front-end with some js framework, e.g., jquery.

Upvotes: 0

pythad
pythad

Reputation: 4267

You may try to chain 2 querysets together and the apply sorted to them:

from itertools import chain
from operator import attrgetter

articles = list(Articles.objects.all())

statut = list(Statut.objects.all())

result_list = sorted(
    chain(articles, statut),
    key=attrgetter('date_created')) # date_created name may differ

Upvotes: 3

Related Questions