1

Following this official tutorial I've coded this:

#! /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="[email protected]",first_name='john', last_name='doe')
john.save()

But I don't know why when trying to run it it says:

/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]

Where I went wrong? I've followed the official tutorial..Did I miss something?

PS Mongodb is up and running

1
  • There's nothing really 'wrong'. Future warnings are a common idiom in Python and other languages to alert you about things that will be deprecated.
    – Petri
    Nov 6, 2012 at 9:24

3 Answers 3

3

The future warning is there to alert you to changes in future versions of MongoEngine that will need addressing before you upgrade.

The warning is:

ReferenceFields will default to using ObjectId strings in 0.8, set DBRef=True if this isn't desired

What that means is - if you dont want to change and migrate your data you should change the definition to:

ReferenceField(User, dbref=False)  # Uses the original way of storing dbrefs
ReferenceField(User, dbref=True)   # Uses a simpler way of storing dbrefs
2
  • What is rationale of moving away from DBRef?
    – Mitar
    May 18, 2013 at 4:44
  • Space - ObjectId's are preferred over DBRefs
    – Ross
    May 20, 2013 at 7:58
1

Your code is using Comment before it is defined. Order matters in Python.

See for example https://stackoverflow.com/a/2985085/1256394.

1
  • You're right, but this does not fixes everything See first post
    – gaggina
    Nov 4, 2012 at 14:51
1

Well, if you don't like that warnings, you can make Post to look like:

class Post(Document):
    meta = {'allow_inheritance': True}
    ...
    author = ReferenceField(User, reverse_delete_rule=CASCADE, dbref=False)
    ...

See http://mongoengine-odm.readthedocs.org/en/latest/upgrade.html and https://github.com/hmarr/mongoengine/issues/437 for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.