Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will create abstract model at the Mongoengine?

I need create class which must inherits from others classes. I try do this as structure as:

class A(Document):
    field_1 = ...
    field_2 = ...

class B(Document):
    field_a = ... 
    field_b = ... 

class C(A,B):
    specific_field_1 = ...
    specific_field_2 = ...

    meta = {
        'collection': 'class_c',
    }

but I don't know decision is complied with the rules. At DBs I will not want collections from class A and class B.

Please, can anybody help me do it right?

like image 847
Abbasov Alexander Avatar asked Nov 05 '25 18:11

Abbasov Alexander


1 Answers

Here's an example on how to create an abstract model in mongoengine (similar to django, by the way):

class A(Document):
    meta = {
        'abstract': True
    }
    pass

class B(Document):
    meta = {
        'abstract': True
    }
    pass

class C(A, B):
    specific_field_1 = ...
    specific_field_2 = ...

    meta = {
        'collection': 'class_c',
    }

Hope that helps.

like image 89
alecxe Avatar answered Nov 07 '25 11:11

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!