I am wondering since ages how to achieve the following:
I have an abstract class wrapping several attributes shared by multiple models. But not every model needs every attribute in exactly the same way.
Here is an example: MyModelA
and MyModelB
both have two fields: value_1
and value_2
. But while MyModelA
needs them to be required/not nullable, they can be nullable for MyModelB
.
class MyAbstractModel(models.Model):
value_1 = models.IntegerField()
value_2 = models.IntegerField()
class Meta:
abstract = True
What I tried:
Not deriving
MyAbstractModel
frommodels.Model
but fromobject
like a regular mixinSetting a class attribute which is overwritten in the child classes to determine the nullable-state. It always takes the definition from
MyAbstractModel
.Using a class attribute but not set it in the
MyAbstractModel
. Then themakemigrations
command fails because of the undefined variable.Cry
Being DRY is such an important paradigm in django, I really wonder that there is nothing on this topic in the internet.
Thanks in advance!