用于mongodb默认对象ID的串行器,在Django rest框架中有不同的字段名称?

时间:2015-03-10 19:51:14

标签: django django-rest-framework mongoengine

我想从' 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')

1 个答案:

答案 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