Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError with Django REST Framework and MongoEngine

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...

like image 377
evermean Avatar asked Dec 20 '22 04:12

evermean


1 Answers

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....

like image 94
evermean Avatar answered Feb 01 '23 22:02

evermean