Form Validation and unique_together on update a model

17 views
Skip to first unread message

Massimo Barbierato

unread,
Apr 13, 2012, 12:08:35 PM4/13/12
to django...@googlegroups.com
Hi all, i'm new. I searched  in the group for answers to my problem, but i didn't find anything :(

My problem is this:

i have a model with two fields in unique_together, the related ModelForm and the view.
When i pass an instance of my model to the form to update it and then save it, i receive an error that says me that there is already a row with the specified fields.
I can't understand why.

Here you can read my code: http://pastebin.com/vDiHvpiV

Thanks a lot.

Tom Evans

unread,
Apr 13, 2012, 2:00:55 PM4/13/12
to django...@googlegroups.com

(I'm speculating a little)

In your code snippet, lines 36-38:

sale = None
if 'edit' in request.GET:
sale = Sales.objects.get(sale_id=request.GET['edit'])

When you submit the form again, 'edit' is not in request.GET, so
'sale' never gets a value. When you then subsequently save the form,
it tries to save a new instance, which fails because it already
matches a row in the database.

Cheers

Tom

Massimo Barbierato

unread,
Apr 13, 2012, 2:59:12 PM4/13/12
to django...@googlegroups.com
Thanks Tom, you're right!!!
I solved it passing 'edit' again.

Thanks again

Max
Reply all
Reply to author
Forward
0 new messages