Opened 5 months ago

Last modified 5 months ago

#28283 assigned Bug

_changeform_view sends wrong object after ModelForm validation

Reported by: ddein Owned by: ddein
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I noticed strange behavior when changing the form of the admin. If I try to save the form with an error, some functions (get_fieldsets, get_prepopulated_fields, get_readonly_fields, get_inline_formsets, something else) get the wrong object after validation. See here: https://github.com/django/django/blob/master/django/contrib/admin/options.py#L1436-L1437
It seems to me not obvious, that after call is_valid, object obj will be changed. The quick solution for me was the following:

form = ModelForm(request.POST, request.FILES, instance=copy.copy(obj))

But maybe the problem elsewhere. Has anyone encountered a similar one, is this a mistake or normal behavior?

Change History (3)

comment:1 Changed 5 months ago by Tim Graham

Triage Stage: UnreviewedAccepted

I think this is related to this note in the documentation:

The cleaning process modifies the model instance passed to the ModelForm constructor in various ways. For instance, any date fields on the model are converted into actual date objects. Failed validation may leave the underlying model instance in an inconsistent state and therefore it’s not recommended to reuse it.

comment:2 in reply to:  1 Changed 5 months ago by ddein

Replying to Tim Graham:

I think this is related to this note in the documentation:

Alright, I can check my code and not allow this error. But we are talking about the code that is used in administration and obviously a correction is needed here.

comment:3 Changed 5 months ago by ddein

Owner: changed from nobody to ddein
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top