Mongodb Mongoengine:如何使用另一个模型的id而不是整个嵌入的json来设置对该模型的引用?

Mongodb Mongoengine:如何使用另一个模型的id而不是整个嵌入的json来设置对该模型的引用?,mongodb,django-rest-framework,mongoengine,Mongodb,Django Rest Framework,Mongoengine,我正在尝试设置我的应用程序,它使用mongodb作为后端,mongoengine位于其之上。还试图使用DRF将其公开为RESTAPI。我想我这里有一个非常直截了当的问题,因为我是这里的新手-如何在mongoengine模型中设置ReferenceField,当通过DRF公开时,它只被ID引用,而不是作为一个完整的嵌入对象引用 以下是我的简单设置: models.py- connect('displent-dev') class PhotoFull(Document): path = S

我正在尝试设置我的应用程序,它使用mongodb作为后端,mongoengine位于其之上。还试图使用DRF将其公开为RESTAPI。我想我这里有一个非常直截了当的问题,因为我是这里的新手-如何在mongoengine模型中设置ReferenceField,当通过DRF公开时,它只被ID引用,而不是作为一个完整的嵌入对象引用

以下是我的简单设置:

models.py-

connect('displent-dev')

class PhotoFull(Document):
    path = StringField()
    title = StringField()
    owner = ReferenceField('UserFull')
    location = StringField()
    date_taken = DateTimeField()
    tags = ListField(StringField())
    focal_length = IntField()


class UserFull(Document):
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
    profile_pic = StringField()
    member_since = DateTimeField()
    membership = StringField()
    theme_pic = StringField()
    photos = ListField(ReferenceField(PhotoFull, reverse_delete_rule=CASCADE))
serializers.py:

class PhotoFullSerializer(MongoEngineModelSerializer):

    class Meta:
        model = PhotoFull
        exclude = ()

class UserFullSerializer(MongoEngineModelSerializer):
    class Meta:
        model = UserFull
        exclude = ()
现在,当我转到url-localhost:3223/api/photos(相应的url已就位)时,我得到了相关模型的完整嵌入,而不仅仅是它们的ID:

[
    {
        "id": "5446ba4cbc8ae30728b87b23", 
        "path": "media/pic3.jpeg", 
        "title": "Walk in the field", 
        "owner": {
            "profile_pic": "media/profilePic.jpg", 
            "first_name": "Dave", 
            "last_name": "Gordon", 
            "member_since": "2014-01-03T00:00:00", 
            "photos": [
                {
                    "date_taken": "Max recursion depth exceeded", 
                    "title": "Max recursion depth exceeded", 
                    "tags": "Max recursion depth exceeded", 
                    "location": "Max recursion depth exceeded", 
                    "focal_length": "Max recursion depth exceeded", 
                    "owner": "5441b6b2bc8ae304d4e6c10e", 
                    "path": "Max recursion depth exceeded", 
                    "id": "Max recursion depth exceeded"
                }
            ], 
            "membership": "Silver", 
            "theme_pic": "media/profilePic.jpg", 
            "id": "5441b6b2bc8ae304d4e6c10e"
        }, 
        "location": "Tulip Fields", 
        "date_taken": "2014-09-24T00:00:00", 
        "tags": [
            "Tulips", 
            "Angel", 
            "Sunset"
        ], 
        "focal_length": 90
    }, 
]

/api/users/
的反向调用不会将照片生成为
objectid的数组。我从DRF文档(在UserFullSerializer中)中找到了以下内容:

当我这样做时,我得到一个错误,ObjectId(“..”)是不可JSON序列化的。当

slug_field="title"

非常感谢您的帮助。这件事再次使我的前端开发陷入停滞。

我不确定对象是如何返回的,但您很可能可以通过覆盖
PhotoSerializer
类上的
所有者
字段来实现这一点

class PhotoFullSerializer(MongoEngineModelSerializer):
    owner = serializers.CharField(source="owner.id", read_only=True)

    class Meta:
        model = PhotoFull
        exclude = ()

这应该只返回
owner
对象的
id
,而不是整个对象。

。这是一个简单的解决办法。这是一种魅力。在mongoengine文档中从未遇到过这种情况。你不知道这个小东西是如何解决了一大堆问题,比如多米诺骨牌效应:)非常感谢!这是Django REST框架的一个特性,其中字段只能返回对象的特定部分(例如
id
)。很高兴知道它能起作用。:)回答得很好。但是,如何使其可写?我的意思是,使用这个序列化程序用字符串输入更新引用字段?@Kevin Brown。我确信我又错过了一些东西。紧急求救信号
class PhotoFullSerializer(MongoEngineModelSerializer):
    owner = serializers.CharField(source="owner.id", read_only=True)

    class Meta:
        model = PhotoFull
        exclude = ()