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