4

I am trying to use Django and the Django REST Framework together with MongoEngine but it doesn't seem to work for me. I don't know where things go wrong ... perhaps someone can help me out. Here is the code:

models.py

from mongoengine import *

class Lady(Document):
    firstname = StringField()
    lastname = StringField()

serializers.py

from rest_framework import serializers
from mongoengine import *

class LadySerializer(serializers.Serializer):

    firstname = serializers.CharField(max_length=50)
    lastname = serializers.CharField(max_length=50)

    def restore_object(self,attrs,instance=None):
        if instance:
            instance.firstname = attrs.get('firstname', instance.firstname)
            instance.lastname = attrs.get('lastname', instance.lastname)
            return instance
        return Lady(**attrs)

Now I test if the serialization works by using the interactive console. I execute the following commands.

from core.models import * 
from core.serializers import *
tiger = Lady(firstname='Tiger', lastname="Lily")
serial = LadySerializer(tiger)
serial.data

what i get is:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/evermean/Code/django/env/pourl/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 499, in data
self._data = [self.to_native(item) for item in obj]
File "/home/evermean/Code/django/env/pourl/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 306, in to_native
value = field.field_to_native(obj, field_name)
File "/home/evermean/Code/django/env/pourl/local/lib/python2.7/site-packages/rest_framework/fields.py", line 164, in field_to_native
value = get_component(value, component)
File "/home/evermean/Code/django/env/pourl/local/lib/python2.7/site-packages/rest_framework/fields.py", line 56, in get_component
val = getattr(obj, attr_name)
AttributeError: 'str' object has no attribute 'firstname'

Now I don't really know why this is happening since there is a firstname attribute in the Lady class? What am I missing here?

Thanks...

2
  • how are you calling the serializer in your code? Commented Jun 22, 2013 at 17:33
  • serial = LadySerializer(tiger) and then serial.data ... Commented Jun 22, 2013 at 18:17

1 Answer 1

11

Finally got the solution. I needed to explicitly set many=False to make it work. So this works fine:

from core.models import * 
from core.serializers import *
tiger = Lady(firstname='Tiger', lastname="Lily")
serial = LadySerializer(tiger, many=False)
serial.data

and yields:

{'firstname': u'Tiger', 'lastname': u'Lily'}

You can find some additional information regarding this problem here. The interesting part for this case is the following post:

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.

Hope this helps....

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.