On Fri, Aug 12, 2016 at 9:58 AM, Tim Graham <[email protected]> wrote:
> A change in Django 1.10 inadvertently caused the following behavior change:
>
> class M(models.Model):
> f = models.CharField(max_length=255, default='default_value')
>
> class MF(forms.ModelForm):
> f = forms.CharField(required=False)
>
> class Meta:
> model = M
> fields = ['f']
>
> >>> mf = MF({})
> >>> m = mf.save()
> >>> m.f
> '' (Django 1.10+)
> 'default_value' (Django < 1.10)
>
>
> On Django < 1.10, {'f': ''} as the form data (rather than empty form
> data) also results in a model instance with the model field default rather
> than an empty string. Claude and I agree that for this latter case,
> transforming an empty POST value to a model field default doesn't seem
> correct (that is, the new behavior seems better).
>
That certainly sounds incorrect to me and I would not want to see pre-1.10
behavior restored for the blank ('') value case.
> However, the desired behavior in the empty POST data case isn't so clear.
> For example, I could imagine an API call where it would be useful for the
> model to use the default if a user doesn't submit any data for that field.
> An issue with the idea that values not present in POST data fallback to
> their default is for HTML widgets like checkboxes. If a checkbox isn't
> checked, the key won't appear in POST data. In such a case, it's
> inappropriate for an unchecked checkbox to fallback to the model field's
> default if it's True. A draft patch to restore the Django < 1.10 behavior
> [1] special cases this with isinstance(form[f.name].field.widget,
> CheckboxInput) to fix some test failures but that hardly seems ideal.
>
> Do you feel we should try to restore the Django < 1.10 behavior or keep
> the new behavior and document that model field defaults are used to
> populate initial blank forms but not to fill missing data from the form
> input?
>
I tend to prefer the more cautious approach, i.e., allow the application to
supply default values to the form if they're not included in the POST, but
it sounds like I might be in the minority.
Tobias
--
*Tobias McNulty*Chief Executive Officer
[email protected]
www.caktusgroup.com
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/CAMGFDKS5YMejS2494teExxV-UJmTqX36eHa4Bq5p6b%2Bk%2B64U_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.