Fixed #6641 -- If we lose a race when creating a new object in get_or_create,

re-get the result from the database and return that. Thanks, Jeff Guinness and
Timothée Peignier.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@7289 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-03-18 14:08:40 +00:00
parent e5342282cc
commit 1b331f6c1e
1 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,5 @@
from django.conf import settings
from django.db import connection, transaction
from django.db import connection, transaction, IntegrityError
from django.db.models.fields import DateField, FieldDoesNotExist
from django.db.models import signals, loading
from django.dispatch import dispatcher
@ -285,11 +285,14 @@ class _QuerySet(object):
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
try:
params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
params.update(defaults)
obj = self.model(**params)
obj.save()
return obj, True
except IntegrityError, e:
return self.get(**kwargs), False
def latest(self, field_name=None):
"""