2

I have the following Python code to connect to a MongoDB (2.0.1 installed through MacPorts) database using MongoEngine (0.7.9 installed through pip):

import datetime
from mongoengine import *

connect('mydb')

class Post(EmbeddedDocument):
    title = StringField(required=True)
    description = StringField(required=True)
    author = StringField(required=True)
    pub_date = DateTimeField(required=True, default=datetime.datetime.now)

p = Post()
p.author = "genba"
p.title = "Test post"
p.description = "This is a test post"

When calling the method to_mongo on the objet p, I get the following traceback:

env/lib/python2.7/site-packages/mongoengine/base.pyc in to_mongo(self)
   1040         # Only add _cls and _types if allow_inheritance is not False
   1041         if not (hasattr(self, '_meta') and
-> 1042            self._meta.get('allow_inheritance', ALLOW_INHERITANCE) == False):
   1043             data['_cls'] = self._class_name
   1044             data['_types'] = self._superclasses.keys() + [self._class_name]

AttributeError: 'NoneType' object has no attribute 'get'

As you may notice, the error is produced by a line of code in MongoEngine's code, installed in the virtual environment env with virtualenv.

This error actually arises both when I embed the document Post into another document by using an EmbeddedDocumentField, and when I just paste the previous code into the Python interpreter (until now, I've just used IPython instead of the vanilla Python interpreter).

I've tried debugging with pdb and searching the internet, but I haven't got to find any useful information on this issue. The only thing I can tell is that self._meta is None and that causes the exception to be raised. But why it is None or what that should mean… I don't know.

Why do I get this error? It's definitely in MongoEngine's code, but is it cause by MongoEngine or by my code/data? How can I solve it?

Thanks in advance.

PS: As additional information, I plan to use this with Django, but the previous code can be run apart from Django, and I've done my tests like that (without Django, just with IPython, like I mentioned before).

8
  • What version of MongoEngine are you running - all that is covered by automated tests, so I'm thinking it might be a version or setup issue.
    – Ross
    Apr 11, 2013 at 10:01
  • Sorry I forgot. It's MongoEngine 0.7.9. I've just edited the question and added the version numbers.
    – Genba
    Apr 11, 2013 at 15:15
  • self._meta must be None Apr 11, 2013 at 15:17
  • Yes, self._meta is None, but why and what should that mean? I don't know. That is actually what is causing the exception to be raised, and what should be fixed. I cannot, however, just add a try…catch into MongoEngine's code or something like that, because I don't really know what that code is supposed to do. Any suggestions?
    – Genba
    Apr 11, 2013 at 17:14
  • Should I open a ticket at MongoEngine's GitHub page?
    – Genba
    Apr 11, 2013 at 17:16

0

Your Answer

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

Browse other questions tagged or ask your own question.