Suppose I want to create and update a model. What fields are displayed and the type of validation depends on the action (create or update). But they still share a lot of the same validation and functality. Is there a clean way to have a ModelForm handle this (besides just if instance exists everywhere) or should I just create two different model forms?
Share
Two possibilities spring to mind. You could set an attribute in the form’s
__init__method, either based on a parameter you explicitly pass in, or based on whetherself.instanceexists and has a non-None pk:The other option is to subclass your modelform – keep the joint functionality in the base class, then the specific create or update functionality in the subclasses.