我已经开始用 mongodb 在 django 中编写一个应用程序(我的第一次)。但是我收到了与我的 DRF-mongoengine 序列化程序相关的错误。错误内容如下:
AttributeError: 'UserSerializer' object has no attribute '_writable_fields'
全回溯如下:
Traceback (most recent call last):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
web_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
web_1 | return view_func(*args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 85, in view
web_1 | return self.dispatch(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/views.py", line 407, in dispatch
web_1 | response = self.handle_exception(exc)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/views.py", line 404, in dispatch
web_1 | response = handler(request, *args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 20, in create
web_1 | serializer.is_valid(raise_exception=True)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 186, in is_valid
web_1 | self._validated_data = self.run_validation(self.initial_data)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 364, in run_validation
web_1 | value = self.to_internal_value(data)
web_1 | File "/usr/local/lib/python2.7/site-packages/rest_framework_mongoengine/serializers.py", line 197, in to_internal_value
web_1 | for field in self._writable_fields:
web_1 | AttributeError: 'UserSerializer' object has no attribute '_writable_fields'
这似乎是 DRF-mongoengine 版本的一些问题,因为当我使用 3.3.0 时,我遇到了一个错误,即没有名为“get_field_names”的属性。为了解决这个问题,我移到了最新版本 3.3.1,并开始获得这个版本。我的 requirements.txt 文件是:
blinker == 1.1
Django==1.8.0
django-rest-framework-mongoengine==3.3.1
gunicorn==19.6.0
django-admin-generator==2.0.0
pymongo==2.8
mongoengine==0.9.0
djangorestframework==3.0.4
我的 serializers.py 文件是:
from rest_framework_mongoengine import serializers
from api.models import User
class UserSerializer(serializers.DocumentSerializer):
class Meta:
model = User
fields = '__all__'
我的模型是:
from mongoengine import *
class User(Document):
name = StringField(max_length=50, required=True)
email = EmailField(max_length=254, required=True)
password = StringField(max_length=100, required=True)
role = StringField(max_length=16, default='basic', required=True)
def __unicode__(self):
return self.name, self.email
关于会发生什么问题的任何想法?