Opened 12 years ago

Last modified 12 years ago

#18548 closed Cleanup/optimization

View changes field when 'unique_together' is not satisfied in a Form — at Version 1

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 Aymeric Augustin)

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.

Change History (1)

comment:1 by Aymeric Augustin, 12 years ago

Description: modified (diff)

Fixed formatting (please use preview).

Note: See TracTickets for help on using tickets.
Back to Top