Manytomany field in Django mongoengine Document

我的未来我决定 提交于 2019-12-10 22:58:27

问题


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

I want to add a manytomany field of Feedback in Company

Thanks in advance.


回答1:


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

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



来源:https://stackoverflow.com/questions/25567083/manytomany-field-in-django-mongoengine-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!