Skip to content

Commit 762d3be

Browse files
CodingWithSakshamsarahboyce
authored andcommitted
Fixed #36581 -- Updated serialization examples from XML to JSON.
1 parent 499fe53 commit 762d3be

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

docs/topics/serialization.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ At the highest level, you can serialize data like this::
1919

2020
from django.core import serializers
2121

22-
data = serializers.serialize("xml", SomeModel.objects.all())
22+
data = serializers.serialize("json", SomeModel.objects.all())
2323

2424
The arguments to the ``serialize`` function are the format to serialize the
2525
data to (see `Serialization formats`_) and a
@@ -31,16 +31,16 @@ almost always be a QuerySet).
3131

3232
You can also use a serializer object directly::
3333

34-
XMLSerializer = serializers.get_serializer("xml")
35-
xml_serializer = XMLSerializer()
36-
xml_serializer.serialize(queryset)
37-
data = xml_serializer.getvalue()
34+
JSONSerializer = serializers.get_serializer("json")
35+
json_serializer = JSONSerializer()
36+
json_serializer.serialize(queryset)
37+
data = json_serializer.getvalue()
3838

3939
This is useful if you want to serialize data directly to a file-like object
4040
(which includes an :class:`~django.http.HttpResponse`)::
4141

42-
with open("file.xml", "w") as out:
43-
xml_serializer.serialize(SomeModel.objects.all(), stream=out)
42+
with open("file.json", "w") as out:
43+
json_serializer.serialize(SomeModel.objects.all(), stream=out)
4444

4545
.. note::
4646

@@ -58,7 +58,7 @@ specify a ``fields`` argument to the serializer::
5858

5959
from django.core import serializers
6060

61-
data = serializers.serialize("xml", SomeModel.objects.all(), fields=["name", "size"])
61+
data = serializers.serialize("json", SomeModel.objects.all(), fields=["name", "size"])
6262

6363
In this example, only the ``name`` and ``size`` attributes of each model will
6464
be serialized. The primary key is always serialized as the ``pk`` element in
@@ -94,7 +94,7 @@ model will be serialized. For example, consider the following models::
9494

9595
If you only serialize the Restaurant model::
9696

97-
data = serializers.serialize("xml", Restaurant.objects.all())
97+
data = serializers.serialize("json", Restaurant.objects.all())
9898

9999
the fields on the serialized output will only contain the ``serves_hot_dogs``
100100
attribute. The ``name`` attribute of the base class will be ignored.
@@ -103,14 +103,14 @@ In order to fully serialize your ``Restaurant`` instances, you will need to
103103
serialize the ``Place`` models as well::
104104

105105
all_objects = [*Restaurant.objects.all(), *Place.objects.all()]
106-
data = serializers.serialize("xml", all_objects)
106+
data = serializers.serialize("json", all_objects)
107107

108108
Deserializing data
109109
==================
110110

111111
Deserializing data is very similar to serializing it::
112112

113-
for obj in serializers.deserialize("xml", data):
113+
for obj in serializers.deserialize("json", data):
114114
do_something_with(obj)
115115

116116
As you can see, the ``deserialize`` function takes the same format argument as
@@ -133,7 +133,7 @@ data in your serialized representation doesn't match what's currently in the
133133
database. Usually, working with these ``DeserializedObject`` instances looks
134134
something like::
135135

136-
for deserialized_object in serializers.deserialize("xml", data):
136+
for deserialized_object in serializers.deserialize("json", data):
137137
if object_should_be_saved(deserialized_object):
138138
deserialized_object.save()
139139

@@ -146,7 +146,7 @@ If fields in the serialized data do not exist on a model, a
146146
``DeserializationError`` will be raised unless the ``ignorenonexistent``
147147
argument is passed in as ``True``::
148148

149-
serializers.deserialize("xml", data, ignorenonexistent=True)
149+
serializers.deserialize("json", data, ignorenonexistent=True)
150150

151151
.. _serialization-formats:
152152

@@ -667,7 +667,7 @@ Typical usage looks like this::
667667

668668
objs_with_deferred_fields = []
669669

670-
for obj in serializers.deserialize("xml", data, handle_forward_references=True):
670+
for obj in serializers.deserialize("json", data, handle_forward_references=True):
671671
obj.save()
672672
if obj.deferred_fields is not None:
673673
objs_with_deferred_fields.append(obj)

0 commit comments

Comments
 (0)