MongoEngine and Python version for tutorial?

128 views
Skip to first unread message

ja...@feathrapp.com

unread,
Apr 19, 2017, 4:04:43 PM4/19/17
to MongoEngine Users
I'm using MongoEngine v 0.13.0 via venv with python 2.7.  I can't find anywhere what versions the tutorial is assuming.

Here's the issue I'm having that makes me suspect I'm using a different version of something. As I follow along with the official tutorial at http://docs.mongoengine.org/tutorial.html, I run into something strange.  This command

for post in Post.objects(tags='mongodb'):
   
print(post.title)

results in nothing, when I should get `MongoEngine Documentation`. Inspecting the Post object using `dir(Post.objects)` shows me that there is a `title` and a `content` attribute, as expected from the tutorial, but there is no `tags` attribute. 

However, if I run this command instead 

for post in Post(tags='mongodb'):
    print(post.title)


I get the expected printout. Looking at both `dir(Post)` and `dir(Post.objects)`, neither have all the expected attributes (title, author, tags, content).

What version of MongoEngine and of Python does the tutorial assume? If you think I'm having an issue unrelated to versioning, what are your thoughts? Thank you!

Keiron O'Shea

unread,
Apr 20, 2017, 6:39:17 AM4/20/17
to MongoEngine Users
Hey there,

Can you please return the output of..


for post in Post._get_collection().find({}):
   
print post

Cheers.

Keiron O'Shea

unread,
Apr 20, 2017, 7:02:58 AM4/20/17
to MongoEngine Users
Hey there,

Can you please return the output of..


for post in Post._find_collection().find({}):
   
print post

Cheers.


On Wednesday, 19 April 2017 21:04:43 UTC+1, ja...@feathrapp.com wrote:

ja...@feathrapp.com

unread,
Apr 20, 2017, 8:34:13 AM4/20/17
to MongoEngine Users
Keiron, thanks for the quick replies. Below are the outputs. There are triplicates because I've gone through the tutorial a few times and haven't bothered dropping the database recently.

>>> for post in Post._get_collection().find({}):
...     print post
...
{u'content': u'Took a look at MongoEngine today, looks pretty cool.', u'author': ObjectId('58f7ac317a2f2e4352167a8a'), u'_id': ObjectId('58f7ac3e7a2f2e4352167a8b'), u'_cls': u'Post.TextPost', u'title': u'Fun with MongoEngine'}
{u'link_url': u'http://docs.mongoengine.com/', u'author': ObjectId('58f7ac1d7a2f2e4352167a89'), u'_id': ObjectId('58f7ac3e7a2f2e4352167a8c'), u'_cls': u'Post.LinkPost', u'title': u'MongoEngine Documentation'}
{u'content': u'Took a look at MongoEngine today, looks pretty cool.', u'author': ObjectId('58f7ac317a2f2e4352167a8a'), u'_id': ObjectId('58f7b4127a2f2e4352167a8d'), u'_cls': u'Post.TextPost', u'title': u'Fun with MongoEngine'}
{u'link_url': u'http://docs.mongoengine.com/', u'author': ObjectId('58f7ac1d7a2f2e4352167a89'), u'_id': ObjectId('58f7b41e7a2f2e4352167a8e'), u'_cls': u'Post.LinkPost', u'title': u'MongoEngine Documentation'}
{u'content': u'Took a look at MongoEngine today, looks pretty cool.', u'author': ObjectId('58f8a9b37a2f2e4b482e2b93'), u'_id': ObjectId('58f8a9bb7a2f2e4b482e2b94'), u'_cls': u'Post.TextPost', u'title': u'Fun with MongoEngine'}
{u'link_url': u'http://docs.mongoengine.com/', u'author': ObjectId('58f8a99c7a2f2e4b482e2b92'), u'_id': ObjectId('58f8a9bb7a2f2e4b482e2b95'), u'_cls': u'Post.LinkPost', u'title': u'MongoEngine Documentation'}
>>> for post in Post._find_collection().find({}):
...     print post
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Post' has no attribute '_find_collection'
>>>

Keiron O'Shea

unread,
Apr 20, 2017, 8:41:53 AM4/20/17
to MongoEngine Users
Could I take a look at your Posts document schema too? I dare suggest that you may of simply forgotten to add the tag variable!

ja...@feathrapp.com

unread,
Apr 20, 2017, 9:13:17 AM4/20/17
to MongoEngine Users
Below is the entirety of my code, which I copied and pasted directly from the tutorial.  `tags` is definitely in the `Post` schema.  

But I think I've discovered part of the issue.  I went through the tutorial yet again, coping and pasting everything directly, yet again, only this time I added the `tags` attribute the first time `Post` gets defined (unlike the code below), and it has worked. But shouldn't `Post` get overwritten when I define it later? See the code below, `Post` is defined 4 times, per the tutorial, each time making a small change.

>>> from mongoengine import *
>>> connect('tumblelog')
MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True, read_preference=Primary())
>>> class User(Document):
...     email = StringField(required=True)
...     first_name = StringField(max_length=50)
...     last_name = StringField(max_length=50)
...
>>> class Post(Document):
...     title = StringField(max_length=120, required=True)
...     author = ReferenceField(User)
...     meta = {'allow_inheritance': True}
...
>>> class TextPost(Post):
...     content = StringField()
...
>>> class ImagePost(Post):
...     image_path = StringField()
...
>>> class LinkPost(Post):
...     link_url = StringField()
...
>>> class Post(Document):
...     title = StringField(max_length=120, required=True)
...     author = ReferenceField(User)
...     tags = ListField(StringField(max_length=30))
...
>>> class Comment(EmbeddedDocument):
...     content = StringField()
...     name = StringField(max_length=120)
...
>>> class Post(Document):
...     title = StringField(max_length=120, required=True)
...     author = ReferenceField(User)
...     tags = ListField(StringField(max_length=30))
...     comments = ListField(EmbeddedDocumentField(Comment))
...
>>> 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))
...
>>> ross = User(email='ro...@example.com', first_name='Ross', last_name='Lawley').save()
>>> john = User(email='jo...@example.com', first_name='John', last_name='Smithe').save()
>>> post1 = TextPost(title='Fun with MongoEngine', author=john)
>>> post1.content = 'Took a look at MongoEngine today, looks pretty cool.'
>>> post1.tags = ['mongodb', 'mongoengine']
>>> post1.save()
<TextPost: TextPost object>
>>>
>>> post2 = LinkPost(title='MongoEngine Documentation', author=ross)
>>> post2.link_url = 'http://docs.mongoengine.com/'
>>> post2.tags = ['mongoengine']
>>> post2.save()

Keiron O'Shea

unread,
Apr 21, 2017, 4:42:53 AM4/21/17
to MongoEngine Users
Depends on whether you're editing it using its primary key...

Jason Fry

unread,
Apr 21, 2017, 8:31:34 AM4/21/17
to mongoeng...@googlegroups.com
I don't mean an instance of `Post` getting overwritten, I'm talking about the class definition getting overwritten. If I define `Post` with 3 attributes, then later define `Post` with 4 attributes, shouldn't it then have 4 attributes?

 - - -- --- -----
Jason Fry
Customer Success Technician



On Fri, Apr 21, 2017 at 4:42 AM, Keiron O'Shea <keiron...@gmail.com> wrote:
Depends on whether you're editing it using its primary key...

--
You received this message because you are subscribed to a topic in the Google Groups "MongoEngine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoengine-users/V30PeDyIJTY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongoengine-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages