[Answered ]-How Can I Display Multiple Models In a Django ListView?

1👍

You can join QuerySet objects if you change them into lists. Then sort it with lambda. Assuming, that you want to sort by created_at attribute:

def get_context_data(self, **kwargs):
    context = super(MultiModelListView, self).get_context_data(**kwargs)
    list1 = list(Model1.objects.filter(created_by=self.request.user))
    list2 = list(Model2.objects.filter(created_by=self.request.user))
    list3 = list(Model3.objects.filter(created_by=self.request.user))
    list_all = list1 + list2 + list3
    context["list_all"] = sorted(list_all, key=lambda x: x.created_at)
    return context

You can always use reverse=True in sorted function if you need change orientation.

Leave a comment