[Answered ]-Mode Inheritance how to implement ModelForms (Inerithance?)

1👍

If you want to leverage the exclude from TaskForm in HumanTaskForm and extend it, you can inherit the Meta class from TaskForm:

class HumanTaskForm(TaskForm):
    class Meta(TaskForm.Meta):
        model = HumanTask
        exclude = TaskForm.Meta.exclude + ('uuid',)

1👍

You need to inherit the parent Meta as well as.

The child class will inherit/copy the parent Meta class. Any attribute explicitly set in the child meta will override the inherited version. To my knowledge there is no way to extend the parent Meta attributes (ie adding to ‘exclude’).

class AwesomeForm(forms.ModelForm):
    class Meta:
        model = AwesomeModel
        exclude = ('title', )

class BrilliantForm(AwesomeForm)
    class Meta(AwesomeForm):
        model = BrilliantModel

.

print(AwesomeForm.Meta.model)
> AwesomeModel

print(BrilliantForm.Meta.model)
> BrilliantModel

print(AwesomeForm.Meta.exclude)
> ('title', )

print(BrilliantForm.Meta.exclude)
> ('title', )

You could do something like this:

class BrilliantForm(AwesomeForm)
    class Meta(AwesomeForm):
        model = BrilliantModel
        exclude = AwesomeForm.Meta.exclude + ('uuid', )

.

print(BrilliantForm.Meta.exclude)
> ('title', 'uuid')

Leave a comment