You could also put a unique_together statement in your Model. If you do
this, you'll need to update your database as well to enforce the constraint
at the database level.

On Fri, Jun 22, 2012 at 10:52 AM, Daniel Roseman <dan...@roseman.org.uk>wrote:

> On Friday, 22 June 2012 06:23:32 UTC+1, ydjango wrote:
>>
>> I have code as
>>
>> view_a(request):
>>   .... Populate my_a, my_b and my_c...
>>   try:
>>       tp = TP.objects.get(a__exact = my_a, b__exact = my_b)
>>   except TP.DoesNotExist, e:
>>      tp = TP()
>>      tp.a = my_a
>>      tp.b = my_b
>>      tp.c = my_c
>>      tp.save()
>>   else:
>>      tp.c = my_c
>>      tp.save()
>>
>> This view is called by ajax calls. TP is a django model and mysql db.
>> I am using django 1.1.4/ python2.5/mod_wsgi2.5,
>>
>> I find that in, one case, two rows with same value of  a and b has been
>> created in DB. ( value of c was different)
>> What could have caused it?
>>
>> Any clues or hints on how to track down this problem or what to look for
>> will be appreciated.
>>
>>
> You probably have a race condition. You should use the built-in
> `get_or_create` manager method, which helps to minimize that sort of thing
> (it's not perfect, but it's better than coding it yourself).
> --
> DR.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/HTccZPk06t8J.
>
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to