Mongoengine:如何设置引用另一个模型只是其id,而不是整个嵌入式json? [英] Mongoengine: How to setup a reference to another model with just its id and not the entire embedded json?

查看:1468
本文介绍了Mongoengine:如何设置引用另一个模型只是其id,而不是整个嵌入式json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to setup my application that uses mongodb as its backend with mongoengine on top of it. Also trying to expose it as a REST API using DRF. I guess I have a very straight forward question here as I am a novice here - How to setup a ReferenceField in a mongoengine model that when exposed via DRF gets only referenced by its ID and not as an entire embedded object?

以下是我简单的设置:

models.py -

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:

serializers.py:

class PhotoFullSerializer(MongoEngineModelSerializer):

    class Meta:
        model = PhotoFull
        exclude = ()

class UserFullSerializer(MongoEngineModelSerializer):
    class Meta:
        model = UserFull
        exclude = ()

现在,当我去url - localhost:3223 / api /照片(相应的URL被放置到位),我得到相关模型的完全嵌入,而不是只是他们的ids:

Now when I go to the url - localhost:3223/api/photos (corresponding urls are put in place), I get the complete embedding of related models instead of just their ids:

[
    {
        "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 / 的反向调用不会将照片作为 objectIds 。我从DRF文档(In UserFullSerializer)中发现这一点:

The reverse call of /api/users/ does not yield photos as an array of objectIds. I found this from the DRF docs(In UserFullSerializer):

photos = serializers.SlugRelatedField(many=True, read_only=True, slug_field="id")

当我这样做,我得到ObjectId(.. )不是JSON可序列化。当

When I do this, I get the error that ObjectId("..") is not JSON serializable. It works well when

slug_field="title"

任何帮助都非常感激。这个东西再次阻止了我的前端开发。

Any help is really appreciated. This thing has again stalled my front end dev.

推荐答案

我不知道对象如何被返回,但是你很可能通过覆盖 PhotoSerializer 类上的所有者字段来执行此操作。

I'm not exactly sure how the objects are being returned, but you can most likely do this by overriding the owner field on the PhotoSerializer class.

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

    class Meta:
        model = PhotoFull
        exclude = ()

只返回所有者对象的 id ,而不是整个对象。

This should only return the id of the owner object instead of the entire object.

这篇关于Mongoengine:如何设置引用另一个模型只是其id,而不是整个嵌入式json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆