Feature request: wigets attribute for ModelFormMixin class

133 views
Skip to first unread message

Jonas Kiefer

unread,
Feb 17, 2021, 9:53:07 AM2/17/21
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.

Tim Graham

unread,
Feb 17, 2021, 10:07:13 AM2/17/21
to Django developers (Contributions to Django itself)

Adam Johnson

unread,
Feb 17, 2021, 10:18:51 AM2/17/21
to django-d...@googlegroups.com
I'll also chime in to say I'd be against expanding these shortcut parameters. They muddy the purpose of the View class for a small saving in LoC, and go against "TOOWTDI".

--
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-develop...@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.


--
Adam
Reply all
Reply to author
Forward
0 new messages