L
L
Loneattic2020-08-10 08:32:23
Django
Loneattic, 2020-08-10 08:32:23

How to sort chain?

How to sort chain by date whose fields have different names in models?

guides = Guide.objects.filter(author=user).order_by('-publish')[:10]
articles = Article.objects.filter(author=user).order_by('-publish')[:10]
memes = Meme.objects.filter(author=user).order_by('-publish')[:10]
comments = XtdComment.objects.filter(user=user).order_by('-submit_date')[:10]

result_list = sorted(chain(guides, articles, memes, comments), key=lambda instance: instance.publish) # <------- ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-08-10
@Loneattic

Make a property on model objects that will return the values ​​of different fields. File an annotation (qs.annotate()) that will return different fields under the same name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question