关注此官方tutorial我已对此进行了编码:
#! /usr/bin/env python
from mongoengine import *
connect('tumbleblog')
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
class Post(Document):
title = StringField(max_length=120, required=True)
author = ReferenceField(User, reverse_delete_rule=CASCADE)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
class TextPost(Post):
content = StringField()
class ImagePost(Post):
image_path = StringField()
class LinkPost(Post):
link_url = StringField()
john = User(email="example@gmail.com",first_name='john', last_name='doe')
john.save()
但我不知道为什么在尝试运行它时会说:
/Library/Python/2.7/site-packages/mongoengine/fields.py:736: FutureWarning: ReferenceFields will default to using ObjectId strings in 0.8, set DBRef=True if this isn't desired
warnings.warn(msg, FutureWarning)
/Library/Python/2.7/site-packages/mongoengine/base.py:589: FutureWarning: TextPost uses inheritance, the default for allow_inheritance is changing to off by default. Please add it to the document meta.
FutureWarning
/Library/Python/2.7/site-packages/mongoengine/base.py:589: FutureWarning: ImagePost uses inheritance, the default for allow_inheritance is changing to off by default. Please add it to the document meta.
FutureWarning
/Library/Python/2.7/site-packages/mongoengine/base.py:589: FutureWarning: LinkPost uses inheritance, the default for allow_inheritance is changing to off by default. Please add it to the document meta.
FutureWarning
[Finished in 0.2s]
我哪里出错了?我遵循了官方教程。我错过了什么?
PS Mongodb启动并运行
答案 0 :(得分:3)
未来警告会提醒您在升级之前需要解决的MongoEngine未来版本中的更改。
警告是:
ReferenceFields默认使用0.8中的ObjectId字符串,如果不需要则设置DBRef = True
这意味着 - 如果您不想更改和迁移数据,则应将定义更改为:
ReferenceField(User, dbref=False) # Uses the original way of storing dbrefs
ReferenceField(User, dbref=True) # Uses a simpler way of storing dbrefs
答案 1 :(得分:1)
您的代码在定义之前使用Comment。订单在Python中很重要。
答案 2 :(得分:1)
好吧,如果您不喜欢这些警告,可以使Post看起来像:
class Post(Document):
meta = {'allow_inheritance': True}
...
author = ReferenceField(User, reverse_delete_rule=CASCADE, dbref=False)
...
有关详细信息,请参阅http://mongoengine-odm.readthedocs.org/en/latest/upgrade.html和https://github.com/hmarr/mongoengine/issues/437。