Opened 4 years ago

Closed 4 years ago

#18214 closed Cleanup/optimization (fixed)

Serializers do not serialize iterators

Reported by: Moritz Sichert Owned by: nobody
Component: Documentation Version: master
Severity: Normal Keywords: serialization json xml
Cc: alex.ogier@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

According to serialization documentation the given object "can be any iterator that yields Django objects, but it'll almost always be a QuerySet". But it does not work with iterators.

Example:

>>> from django.core import serializers
>>> json_serializer = serializers.get_serializer('json')()
>>> json_serializer.serialize([1, 2, 3])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "trunk/django/core/serializers/base.py", line 46, in serialize
    concrete_model = obj._meta.concrete_model
AttributeError: 'int' object has no attribute '_meta'
>>>
>>> xml_serializer = serializers.get_serializer('xml')()
>>> xml_serializer.serialize([1, 2, 3])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "trunk/django/core/serializers/base.py", line 43, in serialize
    self.start_object(obj)
  File "trunk/django/core/serializers/xml_serializer.py", line 42, in start_object
    raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj))
SerializationError: Non-model object (<type 'int'>) encountered during serialization

One should either change the documentation or make this work.

Attachments (1)

serialization-clarification.diff (1.2 KB) - added by Alex Ogier 4 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 4 years ago by Claude Paroz

Component: Core (Serialization)Documentation
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

that yields Django objects. [1, 2, 3] are not Django objects. Accepting on the base that what are Django objects might be more clearly explained in the documentation.

comment:2 Changed 4 years ago by Alex Ogier

Has patch: set

comment:3 Changed 4 years ago by Claude Paroz

Did you purposely left the first occurrence of "Django objects" in the first paragraph?

comment:4 Changed 4 years ago by Alex Ogier

Cc: alex.ogier@… added

Good point, that wasn't deliberate.

Changed 4 years ago by Alex Ogier

comment:5 in reply to:  3 Changed 4 years ago by Moritz Sichert

Triage Stage: AcceptedReady for checkin

Replying to claudep:

Did you purposely left the first occurrence of "Django objects" in the first paragraph?

I did not realize that it reads 'Django objects'. Thank you for the hint!

comment:6 Changed 4 years ago by Claude Paroz

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top