7👍
✅
from itertools import chain
combined = list(chain(collectionA, collectionB))
json = serializers.serialize('json', combined)
3👍
You cannot combine two querysets to serialize them. If you serialize one queryset, it is actually executed and the queryset data is filled in at that moment. If you only want the data in collection, just get the sets, join them and then serialize the joined collection. Something of the form:
from django.core import serializers
collectionA = list(A.objects.all())
collectionB = list(B.objects.all())
joined_collection = collectionA + collectionB
json = serializers.serialize('json', joined_collection)
Try it, this should work.
- [Django]-ImportError: No module named 'bootstrapform'
- [Django]-Convert all CharField Form Field inputs to lowercase in Django forms
- [Django]-How does Django determine if an uploaded image is valid?
- [Django]-Django rest framework: convert queryset to json response
Source:stackexchange.com