Django list ordering by date

I want to display all the results of these two queries on one page and order them by date. The goal is to mix both results in order to simply display a unique list 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 works. It displays all the results.

Now I am wondering how to order by date for template rendering?

Maybe there is an easier way to do this?

thank

+3


source to share


2 answers


You can try chain

2 queries and apply to them sorted

:



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

      

+2


source


Hmm ... my suggestion is sorting them on frontend with some js structure like jquery.



0


source







All Articles