[Fixed]-Django override model subclass create method

1👍

I would do something like this.

class Base(models.Model):
    field1_base = models.IntegerField()

    def initialize(self, *args, **kwargs):
        self.field1_base = kwargs['field1_base']

    @classmethod
    def create(cls, *args, **kwargs):
        # LOGICS
        self = cls()
        self.initialize(*args, **kwargs)
        return self

class A(Base):
    field2_sub = models.IntegerField()

    def initialize(self, *args, **kwargs):
        super(A, self).initialize(*args, **kwargs)
        self.field2_sub = kwargs['field1_base']

A.create(field2_sub=1, field1_base=2)

Leave a comment