django.core.serializers.get_serializer(format)
You can also use a serializer object directly:
XMLSerializer = serializers.get_serializer("xml") xml_serializer = XMLSerializer() xml_serializer.serialize(queryset) data = xml_serializer.getvalue()
This is useful if you want to serialize data directly to a file-like object (which includes an HttpResponse
):
with open("file.xml", "w") as out: xml_serializer.serialize(SomeModel.objects.all(), stream=out)
Note
Calling get_serializer()
with an unknown format will raise a django.core.serializers.SerializerDoesNotExist
exception.
Please login to continue.