MCPcopy
hub / github.com/django/django / serialize

Method serialize

django/core/serializers/base.py:84–150  ·  view source on GitHub ↗

Serialize a queryset.

(
        self,
        queryset,
        *,
        stream=None,
        fields=None,
        use_natural_foreign_keys=False,
        use_natural_primary_keys=False,
        progress_output=None,
        object_count=0,
        **options,
    )

Source from the content-addressed store, hash-verified

82 stream_class = StringIO
83
84 def serialize(
85 self,
86 queryset,
87 *,
88 stream=None,
89 fields=None,
90 use_natural_foreign_keys=False,
91 use_natural_primary_keys=False,
92 progress_output=None,
93 object_count=0,
94 **options,
95 ):
96 """
97 Serialize a queryset.
98 """
99 self.options = options
100
101 self.stream = stream if stream is not None else self.stream_class()
102 self.selected_fields = fields
103 self.use_natural_foreign_keys = use_natural_foreign_keys
104 self.use_natural_primary_keys = use_natural_primary_keys
105 progress_bar = self.progress_class(progress_output, object_count)
106
107 self.start_serialization()
108 self.first = True
109 for count, obj in enumerate(queryset, start=1):
110 self.start_object(obj)
111 # Use the concrete parent class' _meta instead of the object's
112 # _meta This is to avoid local_fields problems for proxy models.
113 # Refs #17717.
114 concrete_model = obj._meta.concrete_model
115 # When using natural primary keys, retrieve the pk field of the
116 # parent for multi-table inheritance child models. That field must
117 # be serialized, otherwise deserialization isn't possible.
118 if self.use_natural_primary_keys:
119 pk = concrete_model._meta.pk
120 pk_parent = (
121 pk if pk.remote_field and pk.remote_field.parent_link else None
122 )
123 else:
124 pk_parent = None
125 for field in concrete_model._meta.local_fields:
126 if field.serialize or field is pk_parent:
127 if field.remote_field is None:
128 if (
129 self.selected_fields is None
130 or field.attname in self.selected_fields
131 ):
132 self.handle_field(obj, field)
133 else:
134 if (
135 self.selected_fields is None
136 or field.attname[:-3] in self.selected_fields
137 ):
138 self.handle_fk_field(obj, field)
139 for field in concrete_model._meta.local_many_to_many:
140 if field.serialize:
141 if (

Callers 7

test_stream_classMethod · 0.95
test_custom_encoderMethod · 0.95
test_no_indentationMethod · 0.95
test_custom_encoderMethod · 0.95
serializeFunction · 0.45
handleMethod · 0.45

Calls 9

start_serializationMethod · 0.95
start_objectMethod · 0.95
handle_fieldMethod · 0.95
handle_fk_fieldMethod · 0.95
handle_m2m_fieldMethod · 0.95
end_objectMethod · 0.95
end_serializationMethod · 0.95
getvalueMethod · 0.95
updateMethod · 0.45

Tested by 5

test_stream_classMethod · 0.76
test_custom_encoderMethod · 0.76
test_no_indentationMethod · 0.76
test_custom_encoderMethod · 0.76