1

按照这个官方教程,我编写了这个:

#! /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 已启动并运行

4

3 回答 3

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
于 2012-11-05T08:26:47.123 回答
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.htmlhttps://github.com/hmarr/mongoengine/issues/437

于 2012-11-04T15:24:45.087 回答
1

您的代码在定义之前使用了 Comment。Python 中的顺序很重要。

参见例如https://stackoverflow.com/a/2985085/1256394

于 2012-11-04T14:48:37.010 回答