Hi
I have a question regarding how one should handle “partial” forms.
Example model:
Class Process(models.Model):
title = models.CharField(...)
description = models.TextField(...)
date = models.DateField(...)
What i want to be able to do, is to create multiple ways of editing this model.
As example, i want to only show a form with the description field to some users, but all three fields to other users.
I tend to believe that creating multiple very similar modelforms cant be optimal, example:
class ProcessForm(forms.ModelForm):
class Meta:
model = Process
fields = ["title", "description", "date"]
class ProcessDescriptionForm(forms.ModelForm):
class Meta:
model = Process
fields = ["description"]
Therefore ive been experimenting with overwriting init and adding a “active_fields” kwarg:
#views.py
form = ProcessForm(request.POST or None, instance=process, active_fields=request.GET.get('active_fields'))
#forms.py
class ProcessForm(forms.ModelForm):
class Meta:
model = Process
fields = fields = ["title", "description", "date"]
def __init__(self, *args, **kwargs):
active_fields = kwargs.pop("active_fields", None)
super(ProcessForm, self).__init__(*args, **kwargs)
if active_fields:
for field_name in self.fields.copy():
if not field_name in active_fields:
self.fields.pop(field_name, None)
How would you handle such “partial” forms?
Bw. Martin