[Django] #27992: Cleaned form values does not save to model

22 views
Skip to first unread message

Django

unread,
Mar 28, 2017, 5:46:22 PM3/28/17
to django-...@googlegroups.com
#27992: Cleaned form values does not save to model
----------------------------------------+-----------------------------
Reporter: Pavel Patrin | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 1.10
Severity: Normal | Keywords: model, form
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------+-----------------------------
Example

{{{
class MyModel(Model):
my_field = TextField(default='')


class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = ('my_field',)

def clean_my_field(self):
return 'cleaned_value' # Or complex logic


form = MyModelForm({})
form.save()

result = MyModelForm.objects.get()

# This is true
result.my_field != 'cleaned_value'
}}}


This happens, because Django applies model attributes like this:

{{{
def construct_instance(form, instance, fields=None, exclude=None):
# ... code
for f in opts.fields:
# ... code
# Leave defaults for fields that aren't in POST data, except for
# checkbox inputs because they don't appear in POST data if not
checked.
if (f.has_default() and
form[f.name].field.widget.value_omitted_from_data(form.data, form.files,
form.add_prefix(f.name))):
continue
# ... code
# ... code
return instance
}}}

So Django skips fields that has default value, but not present in raw
data. Why?

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

Django

unread,
Mar 28, 2017, 5:50:33 PM3/28/17
to django-...@googlegroups.com
#27992: Cleaned form values does not save to model
------------------------------+--------------------------------------

Reporter: Pavel Patrin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: model, form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

Hi, this isn't the place to ask about Django's design decisions. You can
try asking using the resources linked at
TicketClosingReasons/UseSupportChannels. You can also investigate design
decisions by using `git blame` to find commits and tickets related to
those lines of code.

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

Django

unread,
Mar 28, 2017, 5:53:34 PM3/28/17
to django-...@googlegroups.com
#27992: Cleaned form values does not save to model
------------------------------+--------------------------------------

Reporter: Pavel Patrin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: model, form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Pavel Patrin):

Replying to [comment:1 Tim Graham]:


> Hi, this isn't the place to ask about Django's design decisions. You can
try asking using the resources linked at
TicketClosingReasons/UseSupportChannels. You can also investigate design
decisions by using `git blame` to find commits and tickets related to
those lines of code.

Hello, Tim. Ok, i'll see git-blame. If description of a problem look like
a question - i am sorry, this is not a question, i think this is a bug.
So, could anybody say me that this is a feature, not a bug?

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

Django

unread,
Mar 28, 2017, 6:07:06 PM3/28/17
to django-...@googlegroups.com
#27992: Cleaned form values does not save to model
------------------------------+--------------------------------------

Reporter: Pavel Patrin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: model, form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Tim Graham):

No, "Django skips fields that has default value, but not present in raw
data." isn't a bug -- you can look at this history of that code to
understand its purpose. If you find the relevant commit, you'll probably
also find some documentation about the behavior.

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

Django

unread,
Mar 28, 2017, 6:07:51 PM3/28/17
to django-...@googlegroups.com
#27992: Cleaned form values does not save to model
------------------------------+--------------------------------------

Reporter: Pavel Patrin | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: model, form | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Pavel Patrin):

Replying to [comment:3 Tim Graham]:


> No, "Django skips fields that has default value, but not present in raw
data." isn't a bug -- you can look at this history of that code to
understand its purpose. If you find the relevant commit, you'll probably
also find some documentation about the behavior.

Ok. Thank you.

--
Ticket URL: <https://code.djangoproject.com/ticket/27992#comment:4>

Reply all
Reply to author
Forward
0 new messages