Jonas Kiefer
unread,Feb 17, 2021, 9:53:07 AM2/17/21Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django developers (Contributions to Django itself)
# 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.