ModelForm save call with data and instance

34 views
Skip to first unread message

Constantine

unread,
Jul 5, 2011, 2:17:11 AM7/5/11
to Django users
def view(request)
form = MyForm(request.POST, instance = MyModel(myfiled = 1))
if form.is_valid():
obj = form.save()

but POST will NEVER merged with instance i've looked through stack:

ModelForm's save() https://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L362

save calls:

return save_instance(self, self.instance, self._meta.fields,
fail_message, commit, >>>
construct=False <<<)

BUT construct=False and in save_instance
https://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L68

if construct:
instance = construct_instance(form, instance, fields,
exclude)

in doesn't populate instance with cleaned data

Bug or Feature?

and if it's feature, how can I set values dynamically if it's not
specified in POST

bruno desthuilliers

unread,
Jul 5, 2011, 8:21:53 AM7/5/11
to Django users
On Jul 5, 8:17 am, Constantine <theasp...@gmail.com> wrote:
> def view(request)
>     form = MyForm(request.POST, instance = MyModel(myfiled = 1))
>     if form.is_valid():
>         obj = form.save()
>
> but POST will NEVER merged with instance i've looked through stack:
(snip)
> in doesn't populate instance with cleaned data
>

Err... Actually, yes it does:
https://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L305

Constantine Linnick

unread,
Jul 6, 2011, 2:56:29 AM7/6/11
to django...@googlegroups.com
I Found problem, it was triple collision:
pycharm debugging sideeffect, wrong inheritance in _init_, but most destructive was form fields: it overwrites instance values with None.
(if someone find this post from search 

anyway, thanks for reply

Reply all
Reply to author
Forward
0 new messages