Django中的Manytomany字段mongoengine文

Django中的Manytomany字段mongoengine文

本文介绍了Django中的Manytomany字段mongoengine文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Django模型:

I have the following Django model:

from mongoengine import *
from datetime import datetime

class Company(Document):

    name = StringField(max_length=500)



class Feedback(Document):

    text = StringField(max_length=500)
    is_approved = BooleanField(default=False)
    date = DateTimeField(default=datetime.now())

我想在公司中添加一个manytomany字段的反馈

I want to add a manytomany field of Feedback in Company

提前感谢

推荐答案

这不是一个Django模型,而是一个mongoengine文件 。它没有 ManyToManyField 。相反,您应该在 ListField 中的 ReferenceField 添加到您的公司 class,如下所示:

This is not a Django model, but a mongoengine Document. It does not have ManyToManyField. Instead you should probably add a ReferenceField inside a ListField to your Company class, like this:

class Company(Document):
    name = StringField(max_length=500)
    feedbacks = ListField(ReferenceField(Feedback))

class Feedback(Document):
    text = StringField(max_length=500)
    is_approved = BooleanField(default=False)
    date = DateTimeField(default=datetime.now())

资料来源:

这篇关于Django中的Manytomany字段mongoengine文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:11