Good afternoon, the essence of the problem is this: there are 2 types of models - let it be A and B. There are samples for them, i.e.

a = A.objects.filter(parameters) b = B.objects.filter(parameters) 

Required: you need to get one array of these 2 samples (ie, you need to "glue" them), sorted by a specific field (let there be a 'date' field). After concatenation, the unsorted array is obtained. Sorting by type (man):

  sorted(result, key=operator.attrgetter('date')) sorted(result, key=lambda item: item.date) 

do not lead to anything, the data still remain unsorted. Knowing people, tell me where I have a mistake?

    2 answers 2

    Oh, I earned, I made such a stupid mistake ... I did not assign the result of sorting a variable, but I simply left it as I indicated in the listing. Both ways of working, can someone come in handy, here they are, adjusted:

     from operator import attrgetter sort_result = sorted(result, key=attrgetter('date')) sort_result = sorted(result, key=lambda item: item.date) 

      I do not pretend to answer, but your case is still very interesting. Specifically, it is of interest whether the construction of the following type will work.

       results = A.objects.filter(params) | B.objects.filter(params) results = results.order_by('date') 

      If it works, then here is the option.

      • Sadly, but at the first time limit: AssertionError at / search / Cannot combine queries for two different base models. - metazet
      • Yes, sorry. We see the magic of the genre is not so strong;) - ChehoV
      • Yes, there are enough of the 2 methods mentioned by me above :) - metazet 1:56 pm