I was trying to combine two queryset objects data by itertools chain python predefine function and sorting this with order_by. but i'm getting an AttributeError: 'list' object has no attribute 'order_by'.
If anybody could figure out where i'm doing thing wrong then would be much appreciated. thank you so much in advance.
try:
qs = ChatMessage.objects.filter(
Q(from_user=user, to_user=to_user) |
Q(from_user=to_user, to_user=user)
)
except:
raise ValidationError({"message":"bad request"})
data = UserChatMessageSerializer(qs, many=True, context={'request': request}).data
# qs.update(read_status=True)
for record in data:
data1 = record['conversation']
data2 = record['files']
print(data2)
from itertools import chain
all_results = list(chain(data1,data2)).order_by('-created_on')
output:
"all_results": [
{
"id": 19,
"message": "sad",
"created_on": "2021-01-08T19:43:12.232310Z",
"read_status": false,
"is_edited": false,
"chat": 8
},
{
"id": 20,
"message": "sad",
"created_on": "2021-01-09T06:32:30.381712Z",
"read_status": false,
"is_edited": false,
"chat": 8
},
]