This has been proposed before (closed as wontfix): https://code.djangoproject.com/ticket/24589 https://groups.google.com/g/django-developers/c/34HJqx48h6Y/m/Nm3UMTK4BgAJ
On Wednesday, February 17, 2021 at 9:53:07 AM UTC-5 jonask...@gmail.com wrote: > > # Feature request: wigets attribute for ModelFormMixin class > > ### Status quo > > The `ModelFormMixin` class currently implements the `fields` attribute by > it self and pass it to the `modelform_factory`: > > ```python > class ModelFormMixin(FormMixin, SingleObjectMixin): > """Provide a way to show and handle a ModelForm in a request.""" > fields = None > > def get_form_class(self): > """Return the form class to use in this view.""" > if self.fields is not None and self.form_class: > raise ImproperlyConfigured( > "Specifying both 'fields' and 'form_class' is not > permitted." > ) > if self.form_class: > return self.form_class > else: > ... > return model_forms.modelform_factory(model, fields=self.fields) > ``` > > On generic `CreateView` for example the field can be configured like: > > ```python > class MonitoringRunNewView(CreateView): > model = MonitoringRun > fields = ('metadatas', ) > ... > > ``` > > Same can be done in `UpdateView` and so on. > > ### Enhancement > > Provide the possibility to declare widgets for the configured fields also > like: > > ```python > class ModelFormMixin(FormMixin, SingleObjectMixin): > """Provide a way to show and handle a ModelForm in a request.""" > fields = None > widgets = None > > def get_form_class(self): > """Return the form class to use in this view.""" > if self.fields is not None and self.form_class: > raise ImproperlyConfigured( > "Specifying both 'fields' and 'form_class' is not > permitted." > ) > if self.form_class: > return self.form_class > else: > ... > return model_forms.modelform_factory(model, > fields=self.fields, widgets=self.widgets) > ``` > > ```python > class MonitoringRunNewView(CreateView): > model = MonitoringRun > fields = ('metadatas', ) > widgets = {'metadatas': forms.HiddenInput()} > ... > > ``` > > Configuring of `labels`, `help_texts` and `error_messages` could also be > helpfull. For that we also simply need to pass the attributes as in the > example above described. -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/be430d12-c5d8-46e9-8356-0548ae6cb39en%40googlegroups.com.