[Django] #30407: Modelform initial values override instance values when both are set

6 views
Skip to first unread message

Django

unread,
Apr 26, 2019, 5:12:48 AM4/26/19
to django-...@googlegroups.com
#30407: Modelform initial values override instance values when both are set
---------------------------------------+------------------------
Reporter: Alper Cugun | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------+------------------------
We ran into a bug (on our side) where our data was not being displayed in
a ModelForm for existing instance because we were setting both initial
values as the instance in the ModelForm.

Digging into the documentation it turns out that for [ModelForm initial
overrides instance values
https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#providing-
initial-values] ("Initial values provided this way will override both
initial values from the form field and values from an attached model
instance.") whereas for [normal Forms initial
https://docs.djangoproject.com/en/2.2/ref/forms/api/#dynamic-initial-
values] does not take precedence over bound data ("These values are only
displayed for unbound forms, and they’re not used as fallback values if a
particular value isn’t provided."). Maybe that's my mistake for thinking a
bound Form and a ModelForm with an instance are roughly equivalent.

I tracked this down to [this commit
https://github.com/django/django/commit/51dc4ecf943d1dcc044ed956925760f9d480f56c]
12 years ago and it seems that nobody has complained much about this piece
of code in the mean time. So maybe it isn't a problem but I think there is
something to be said for reversing the precedence in ModelForm.

I've attached a draft diff to fix this.

--
Ticket URL: <https://code.djangoproject.com/ticket/30407>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 26, 2019, 5:13:10 AM4/26/19
to django-...@googlegroups.com
#30407: Modelform initial values override instance values when both are set
-----------------------------+--------------------------------------

Reporter: Alper Cugun | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by Alper Cugun):

* Attachment "modelform_reverse_initial.diff" added.

draft patch

Django

unread,
Apr 26, 2019, 5:14:17 AM4/26/19
to django-...@googlegroups.com
#30407: Modelform initial values override instance values when both are set
-----------------------------+--------------------------------------

Reporter: Alper Cugun | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Description changed by Alper Cugun:

Old description:

> We ran into a bug (on our side) where our data was not being displayed in
> a ModelForm for existing instance because we were setting both initial
> values as the instance in the ModelForm.
>
> Digging into the documentation it turns out that for [ModelForm initial
> overrides instance values
> https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#providing-
> initial-values] ("Initial values provided this way will override both
> initial values from the form field and values from an attached model
> instance.") whereas for [normal Forms initial
> https://docs.djangoproject.com/en/2.2/ref/forms/api/#dynamic-initial-
> values] does not take precedence over bound data ("These values are only
> displayed for unbound forms, and they’re not used as fallback values if a
> particular value isn’t provided."). Maybe that's my mistake for thinking
> a bound Form and a ModelForm with an instance are roughly equivalent.
>
> I tracked this down to [this commit
> https://github.com/django/django/commit/51dc4ecf943d1dcc044ed956925760f9d480f56c]
> 12 years ago and it seems that nobody has complained much about this
> piece of code in the mean time. So maybe it isn't a problem but I think
> there is something to be said for reversing the precedence in ModelForm.
>
> I've attached a draft diff to fix this.

New description:

We ran into a bug (on our side) where our data was not being displayed in
a ModelForm for existing instance because we were setting both initial
values as the instance in the ModelForm.

Digging into the documentation it turns out that for [ModelForm

https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#providing-
initial-values] initial overrides instance values ("Initial values


provided this way will override both initial values from the form field
and values from an attached model instance.") whereas for normal [Forms

https://docs.djangoproject.com/en/2.2/ref/forms/api/#dynamic-initial-
values] initial does not take precedence over bound data ("These values


are only displayed for unbound forms, and they’re not used as fallback
values if a particular value isn’t provided."). Maybe that's my mistake
for thinking a bound Form and a ModelForm with an instance are roughly
equivalent.

I tracked this down to [this commit
https://github.com/django/django/commit/51dc4ecf943d1dcc044ed956925760f9d480f56c]
12 years ago and it seems that nobody has complained much about this piece
of code in the mean time. So maybe it isn't a problem but I think there is
something to be said for reversing the precedence in ModelForm.

I've attached a draft diff to fix this.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30407#comment:1>

Django

unread,
Apr 26, 2019, 5:47:52 AM4/26/19
to django-...@googlegroups.com
#30407: Modelform initial values override instance values when both are set.
-----------------------------+--------------------------------------

Reporter: Alper Cugun | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix
* version: 2.2 => master
* type: Bug => New feature


Comment:

This behavior is documented and expected (as you mentioned above), so a
change will be backward incompatible. You can start a discussion with your
proposal on
[https://groups.google.com/forum/?utm_source=digest&utm_medium=email#!forum
/django-developers/topics django-developers] if you think that this should
be changed.

--
Ticket URL: <https://code.djangoproject.com/ticket/30407#comment:2>

Django

unread,
Apr 26, 2019, 4:54:18 PM4/26/19
to django-...@googlegroups.com
#30407: Modelform initial values override instance values when both are set.
-----------------------------+--------------------------------------

Reporter: Alper Cugun | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------

Comment (by Alper Cugun):

OK. I think I expected as much. Thanks for clearing this up and at least
now it's documented here as well.

--
Ticket URL: <https://code.djangoproject.com/ticket/30407#comment:3>

Reply all
Reply to author
Forward
0 new messages