Brausebart
Brausebart

Reputation: 23

Mongoengine ReferenceField Issue

I want to create a referencefield that takes the same document type as the one where it will be a member of but it does not work and I have no idea how to solve this. Did I forgot something or do I have to do it in another way?

import mongoengine as mongo

class AuthRequest(mongo.EmbeddedDocument):
    user_id = mongo.IntField(required=True, min_value=0)
    message = mongo.StringField(required=True, max_length=256)


class DatabaseUser(mongo.EmbeddedDocument):
    id = mongo.IntField(primary_key=True, min_value=0)
    name = mongo.StringField(required=True, unique=True, max_length=24)
    passw = mongo.StringField(required=True)
    mail = mongo.EmailField(required=True)
    last_online = mongo.DateTimeField()
    contact_field = mongo.ReferenceField('self',
                        reverse_delete_rule=mongo.NULLIFY)
    contacts = mongo.ListField(contact_field)
    requests = mongo.ListField(mongo.EmbeddedDocumentField(AuthRequest))


class UserCollection(mongo.Document):
    users = mongo.ListField(mongo.EmbeddedDocumentField(DatabaseUser))
    meta = {'collection': 'users'}

This is the error I get when using "DatabaseUser":

Traceback (most recent call last):
  File "path_to_my_script.py", line 206, in <module>
    class DatabaseUser(mongo.EmbeddedDocument):
  File "E:\Python27\lib\site-packages\mongoengine\base.py", line 401, in __new__
    field.document_type.register_delete_rule(new_class, field.name,
  File "E:\Python27\lib\site-packages\mongoengine\fields.py", line 605, in document_type
    self.document_type_obj = get_document(self.document_type_obj)
  File "E:\Python27\lib\site-packages\mongoengine\base.py", line 42, in get_document
    """.strip() % name)
mongoengine.base.NotRegistered: `DatabaseUser` has not been registered in the document registry.
            Importing the document class automatically registers it, has it
            been imported?

And this is the error when using "self":

Traceback (most recent call last):
  File "path_to_my_script.py", line 206, in <module>
    class DatabaseUser(mongo.EmbeddedDocument):
  File "E:\Python27\lib\site-packages\mongoengine\base.py", line 401, in __new__
    field.document_type.register_delete_rule(new_class, field.name,
AttributeError: type object 'DatabaseUser' has no attribute 'register_delete_rule'

I am using Python 2.7.2 (64bit) and Mongoengine v.5 on Windows 7 (64bit).

Upvotes: 2

Views: 3578

Answers (1)

Ross
Ross

Reputation: 18111

Currently, reference fields only support document references not embedded documents.

Upvotes: 1

Related Questions