#18548 closed Cleanup/optimization (fixed)
View changes field when 'unique_together' is not satisfied in a Form
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | timograham@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I have a Model called Person with the following constraint: unique_together = ('name', 'country')
Furthermore I have a view to to edit a Person:
def edit_climbing_place(request, person_id): person = get_object_or_404(Person, pk=person_id) print person.name if not request.method == 'POST': form = Person(instance=place) else: form = Person(request.POST, instance=place) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(start_page)) print person.name return render_to_response('myProject/edit_person.html', {'form' : form, 'person': person}, context_instance=RequestContext(request))
In the above code if form.is_valid() fails due to the 'unique_together' constraint is not satisfied then the two 'print person.name' outputs are different. In other words if I edit an existing 'Person' (say A) and change the name to a name which matches an already existing person (say B) in the same 'country' then in the above view the variable 'person.name' changes to that of B. All other fields, however, stay the same.
Attachments (1)
Change History (9)
comment:1 by , 12 years ago
Description: | modified (diff) |
---|
comment:2 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
You must have edited the code before posting it. As is it will crash for at least two reasons:
- the
Person
class is used both as a model and as a form, - the
place
variable is used but is never defined.
The code you've posted can't work and the result you're describing sounds reasonable. I can't guess what your actual code looks like nor why you think the result isn't correct.
In short, this appears to be a support question rather than a bug in Django. Please follow the instructions on this page: TicketClosingReasons/UseSupportChannels. Thanks!
comment:3 by , 12 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Sorry i did edit the code to simplify it a bit but i apparently did it too fast. This should be more clear:
def edit_person(request, person_id): person = get_object_or_404(Person, pk=person_id) print person.name if not request.method == 'POST': form = PersonForm(instance=person) else: form = PersonForm(request.POST, instance=person) if form.is_valid(): form.save() return HttpResponseRedirect(reverse(start_page)) print person.name return render_to_response('myProject/edit_person.html', {'form' : form, 'person': person}, context_instance=RequestContext(request))
comment:4 by , 12 years ago
I think this is a duplicate of #12896, which was ultimately "fixed" via a documentation note. Possibly that note needs to be extended to indicate that the model cleaning in-place means that part of the requested changes may be applied to the instance before form validation ultimately fails and causes is_valid()
to return False?
comment:5 by , 12 years ago
Component: | Forms → Documentation |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
Version: | 1.3 → 1.4 |
Yes, the documentation could state more clearly that the *instance* passed to the form will be modified and should not be reused later in the view, especially if validation fails.
by , 12 years ago
Attachment: | 18548.diff added |
---|
comment:6 by , 12 years ago
Cc: | added |
---|---|
Has patch: | set |
I added the note in the referenced ticket so I think it's clear, but does the attached patch help emphasize the point?
comment:7 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Fixed formatting (please use preview).