kurtgn
kurtgn

Reputation: 8710

django modelform saving resets object data

I am doing a standard object update via POST request:

order=Order.objects.get(uid=o_uid)
form=OrderForm(request.POST,instance=order)
updated_order=form.save(commit=False)
print(updated_order.uid)

problem is, the updated_order.uid gets reset to None.

My form inherits all fields from the Order object:

class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields=('__all__') 

so it has the possibility to carry uid data. But the POST request doesn't have uid key (because I didn't include this field in the HTML form template)

(Pdb) request.POST
<QueryDict: {'index': ['100100'], 'csrfmiddlewaretoken': ['6iz2chMfgrwmaAUsztC8y216I4eAvefV'],
             'inn': ['123123123123'], 'firma_telefon': ['(123) 123-13-13']}>

so why does it get reset by form.save()? Should I explicitly exclude it from the form fields?

P.S. Of course I should exclude it at least for security reasons, but the question still stands: why does it get reset even if it's not present in the POST request?

Upvotes: 0

Views: 142

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599580

It has to work that way. An empty html form input is not sent in the POST data, so the backend doesn't know the difference between you explicitly sending an empty string to overwrite the current data, it just leaving it blank.

Upvotes: 1

Related Questions