AttributeError与Django REST框架和MongoEngine [英] AttributeError with Django REST Framework and MongoEngine

查看:147
本文介绍了AttributeError与Django REST框架和MongoEngine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Django和Django REST框架与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

models.py

from mongoengine import *

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

serializers.py

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

我得到的是:

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'

现在我不知道为什么会发生这种情况,因为Lady类有一个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?

谢谢...

推荐答案

终于得到了解决方案。我需要明确设置 many = False 使其工作。所以这样做很好:

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

,并得出:

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

您可以找到有关此问题的其他信息< a href =https://github.com/tomchristie/django-rest-framework/issues/564> here 。这个案例的有趣部分是以下文章:

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


版本2.2启动隐式迭代行为的弃用。目前,您需要明确指定 many = False 来强制行为不会迭代 __ iter __ 样式对象。按2.4,默认值将从切换到 False

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

这篇关于AttributeError与Django REST框架和MongoEngine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆