Opened 3 years ago

Closed 3 years ago

#18214 closed Cleanup/optimization (fixed)

Serializers do not serialize iterators

Reported by: moritzs 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 ogier 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by claudep

  • Component changed from Core (Serialization) to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to Cleanup/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 3 years ago by ogier

  • Has patch set

comment:3 follow-up: Changed 3 years ago by claudep

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

comment:4 Changed 3 years ago by ogier

  • Cc alex.ogier@… added

Good point, that wasn't deliberate.

Changed 3 years ago by ogier

comment:5 in reply to: ↑ 3 Changed 3 years ago by moritzs

  • Triage Stage changed from Accepted to Ready 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 3 years ago by claudep

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top