Serializer to_native function checks for __iter__ #564

Closed
ajbuitragom opened this Issue Jan 7, 2013 · 3 comments

Comments

Projects
None yet
2 participants

Hi

The objects I'm trying to serialize implements iter (I'm working witn mongoengie and the object I'm talking about is of type Document).

The method to_native (https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L274) of the class BaseSerializer, checks if the passed object is a unique object or a list and for this purpose it uses the line hasattr(obj,'iter).

For my particular case this method don't work because the implementation of iter in my native objects (mongoengine Document).

I rewrote the function in my serializer as:

def to_native(self, obj):
"""
Serialize objects -> primitives.
"""
if obj.class == QuerySet:
return [self.convert_object(item) for item in obj]
return self.convert_object(obj)

This is very specific for mongoengine.

Is there maybe other way to do this in a more general elegant way?

Thank you

Collaborator

tomchristie commented Jan 7, 2013

Is there maybe other way to do this in a more general elegant way?

At the moment, no.

I think the plan for 2.2 is that you need to explicitly specify many=True
if you want to iterate across multiple objects, and that a serializer will
otherwise treat the input as a single object.
For context, note that the 2.2 change would also mean that fields like
blah = ManyPrimaryKeyRelatedField() would be become deprecated in favor
of `blah = PrimaryKeyRelatedField(many=True)

If you wanted to help implement that change that'd be great, otherwise,
you'll need to stick with the current workaround.

On 7 January 2013 18:24, ajbuitragom notifications@github.com wrote:

Hi

The objects I'm trying to serialize implements iter (I'm working witn
mongoengie and the object I'm talking about is of type Document).

The method to_native (
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/serializers.py#L274)
of the class BaseSerializer, checks if the passed object is a unique object
or a list and for this purpose it uses the line hasattr(obj,'iter).

For my particular case this method don't work because the implementation
of iter in my native objects (mongoengine Document).

I rewrote the function in my serializer as:

def to_native(self, obj):
"""
Serialize objects -> primitives.
"""
if obj.class == QuerySet:
return [self.convert_object(item) for item in obj]
return self.convert_object(obj)

This is very specific for mongoengine.

Is there maybe other way to do this in a more general elegant way?

Thank you


Reply to this email directly or view it on GitHubhttps://github.com/tomchristie/django-rest-framework/issues/564.

Collaborator

tomchristie commented Jan 26, 2013

I'm going to classify this as a bug. The current behavior is as intended, but it's not ideal, and we should really declare explicitly when a serializer is expected to iterate or not.

Collaborator

tomchristie commented Feb 6, 2013

Fixed in master.

Version 2.2 starts the deprecation of the implicit iteration behavior. At the moment you'll need to explicitly specify many=False to force the behavior to not iterate over __iter__ style objects. By 2.4 the default value will switch from None to False.

https://github.com/tomchristie/django-rest-framework/blob/master/docs/topics/2.2-release-notes.md

@tomchristie tomchristie closed this Feb 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment