How to override __new__ metaclass model method in Django

I use Django 1.6and model inheritancein Django. I want to do this by connecting a new class.

This would be done in Python, for example,

class Meta(type):
    def __new__(cls, name, bases, newattrs):
        do_what_you_want_before()
        result= super(Meta, cls).__new__(cls, name, bases, newattrs)
        do_what_you_want_after()
        return result

class Foo:
    __metaclass__ = Meta

class SubFoo(Foo):
    pass

When this piece of code is initialized, a custom method will be called __new__.

How can I do this in Django with a model overlay. When you try to do this using Django models, this error occurs:

TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Thank.

+4
source share
1 answer

Instead of inheriting from typeinherit from django.db.models.base.ModelBase( source ).

+7
source

All Articles