Feature request: wigets attribute for ModelFormMixin class

2021-02-17 Thread Jonas Kiefer

# 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/25e0e4a1-d3f5-4f18-945f-304ea140e22an%40googlegroups.com.


Re: Feature request: wigets attribute for ModelFormMixin class

2021-02-17 Thread Tim Graham
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.


Re: Feature request: wigets attribute for ModelFormMixin class

2021-02-17 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
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".

On Wed, 17 Feb 2021 at 15:07, Tim Graham  wrote:

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


-- 
Adam

-- 
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/CAMyDDM2cGQUHjUA98EERzANeUUCy_mhkV465NAN_A_s_TDXAkg%40mail.gmail.com.


Feature request: Extend Storage.save() to directly save strings/bytes

2021-02-17 Thread guettli


To save strings/bytes you need to use ContentFile up to now:

path = default_storage.save('path/to/file', ContentFile(b'new content'))

Docs:  https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects

Things would be bit easier, if save() would support strings/bytes directly.

This way a developer needs to type and import less code.

The implementation would be simple: Storage.save() could check with 
"isinstance()"

if the value needs to be wrapped in ContentFile.

Would you accept a patch to Storage.save()?

Background: I am writing a unittest which mocked away the storage calls to 
save() will put the data into the mock.

Then I check if the data is the way I want it.

Now it gets complicated again: I need to access the data which is 
inside ContentFile grrr.

This would be much easier if you could add strings/bytes to 
the save() directly.

-- 
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/fabd4661-93b9-45f4-a47e-3ffc31abc50cn%40googlegroups.com.