我想从' id'更改objectId名称到' user_id'在解析django请求时解析。我正在使用django-rest-framework-mongoengine
我的用户序列化程序看起来像这样
class UserSerializer(DocumentSerializer):
user_id = serializers.Field(source='id')
class Meta:
model = User
depth = 2
fields = ('user_id','name','address')
答案 0 :(得分:0)
您应该将字段定义为:
from rest_framework_mongoengine.serializers import DocumentSerializer
from rest_framework_mongoengine.fields import ObjectIdField
from my_app.documents import User
class UserSerializer(DocumentSerializer):
user_id = ObjectIdField(source='id')
class Meta:
model = User
depth = 2
fields = ('user_id','name','address')
不同之处在于你应该使用django-rest-framework-mongoengine' ObjectIdField。