Question

This code bombs:

from mongoengine import *

class Employee(Document):
    name = StringField()
    boss = ReferenceField("Employee", reverse_delete_rule = NULLIFY)

Heres the exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[…]/mongoengine/base.py", line 791, in __new__
    new_class = super_new(cls, name, bases, attrs)
  File "[…]/mongoengine/base.py", line 630, in __new__
    f.document_type.register_delete_rule(new_class,
    File "[…]/mongoengine/fields.py", line 757, in document_type
    self.document_type_obj = get_document(self.document_type_obj)
  File "[…]/mongoengine/base.py", line 136, in get_document
    """.strip() % name)
mongoengine.base.NotRegistered: `Employee` has not been registered
in the document registry.
Importing the document class automatically registers it, has it
been imported?

Removing the reverse_delete_rule fixes the problem, but I would like to have this rule.

I tried this, and it works, but it really looks like crap, and I fear that there might be bad side-effects (so far, I have not seen any, though):

from mongoengine import *

class Employee(Document):
    pass # required for the reverse_delete_rule to work on the boss field,
         # because the Employee class needs to exist.

class Employee(Document):
    name = StringField()
    boss = ReferenceField("Employee", reverse_delete_rule = NULLIFY)

Any ideas? Shouldn't this be considered a bug in MongoEngine?

Was it helpful?

Solution

Try use 'self' instead 'Employee':

from mongoengine import *

class Employee(Document):
    name = StringField()
    boss = ReferenceField("self", reverse_delete_rule = NULLIFY)

See details: https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html#reference-fields.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top