Django mongoengine文件中的Manytomany字段

时间:2014-08-29 11:07:45

标签: python django mongodb mongoengine

我有以下Django模型:

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())

我想在公司中添加许多反馈字段

提前致谢。

1 个答案:

答案 0 :(得分:4)

这不是Django模型,而是mongoengine Document。它没有ManyToManyField。相反,您应该在ReferenceFieldListField内添加Company,如下所示:

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())

来源:http://docs.mongoengine.org/guide/defining-documents.html#one-to-many-with-listfields