我已经开始使用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
知道发生了什么问题?