Reputation: 2509
I want to know how best to dynamically restrict which fields are part of a ModelForm instance. Specifically,
class ModelWithManyFields(models.Model):
many fields ....
class FormOfSelectedModelFields(forms.ModelForm):
class Meta:
model = ModelWithManyFields
fields = [ a dynamic list for model fields determined by the view ]
What is the best way to create an instance of FormOfSelectdModelFields()
with a list of fields dynamically determined by some logic inherent from the view?
Upvotes: 0
Views: 133
Reputation: 1298
Well, you probably want to create something like factory for this ModelForm.
In simple case you probably just want to use https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform-factory-function
Upvotes: 3