Flask mongoengine参考对象的ObjectId

时间:2018-09-02 15:48:51

标签: flask mongoengine flask-mongoengine

我正在使用消息数据模型,其中有一个会话文档,以及链接到每个会话的消息文档。

这是会话课程

class Conversation(db.DynamicDocument):
    # Apparently yo
    _id = db.ObjectIdField()
    post_id = db.StringField(required=True)
    buyer = db.StringField(required=True)
    seller = db.StringField(required=True)
    # MessageParticipants = db.EmbeddedDocumentListField(AuthorInfo)
    # check this later cause even if I store name, which one am I refering to?

    meta = {'collection': 'conversations',
            'indexes': ['$post_id',
                        ],
            'ordering': ['-visits']
            }

对话类别:

class Messages(db.DynamicDocument):
    conversation = db.StringField(required=True)
    # conversation = db.ObjectIdField()
    author = db.StringField(required=True)
    message = db.StringField(required=True)
    time_created = db.DateTimeField(default=datetime.now)

    meta = {'collection': 'messages',
            'indexes': ['$author',
                        '$time_created',
                        'time_created'
                        ],
            'ordering': ['conversation']
            }

我尝试了多种方法:

  1. 将对话类型设为ObjectId
  2. 在对话中使用参考对象
  3. 然后我有了使用String的想法,将ObjectId转换为String

每次测试均失败,并出现以下错误:

  

pymongo.errors.OperationFailure:索引:{v:2,键:{_fts:“文本”,_ftsx:1},名称:“ time_created_text”,ns:   “ biztransfer.messages”,背景:错误,权重:{time_created:1   },default_language:“英语”,language_override:“ language”,   textIndexVersion:3}已经存在不同的选项:{v:2,   键:{_fts:“文本”,_ftsx:1},名称:“ author_text”,ns:   “ biztransfer.messages”,背景:错误,权重:{作者:1},   default_language:“英语”,language_override:“ language”,   textIndexVersion:3}

这是到目前为止我对Flask Shell进行的尝试:

from app.models import Conversation, Messages
onv = Conversation.objects.first()
conv.post_id
msg = Messages(conversation=str(conv._id), author='sdgg', message='sdgdgds')
msg.save()

0 个答案:

没有答案