Abstract class - MongoEngine

Viewed 12

Per MongoEngine docs (http://docs.mongoengine.org/guide/defining-documents.html#abstract-classes:~:text=document._data%20dictionary.-,2.3.9.%20Abstract%20classes,-If%20you%20want) abstract classes is a nice way to provide extra functionality to a group of Document classes.

I am trying to achieve this, but am struggling to create methods that do even simple things as get a document by id. I have something like the following:

class BaseDocument(Document):
    meta = {
        'abstract': True,
    }

    def delete_by_id(self,id):
        document = self.objects(_id=id) # <---- self is a User Object, not the User collection
        document.delete()


class User(BaseDocument):
    _id = StringField(type="string", primary_key=True)
    name = StringField(type="string")
    email = StringField(type="string")

new_user = User(_id="123",name="123",email="123@123.com")
new_user.delete_by_id("123") # <--- throws 'QuerySetManager' object is not callable

is there no way to have the BaseDocument method "delete_by_id" delete a document without having to use the User class itself inside the "delete_by_id"-method? Such an implementation would make the class BaseDocument so much more "inheritable".

Thank you for any help!!

0 Answers
Related