Skip to content

Commit

Permalink
Fixed #6641 -- If we lose a race when creating a new object in get_or…
Browse files Browse the repository at this point in the history
…_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
  • Loading branch information
malcolmt committed Mar 18, 2008
1 parent e534228 commit 1b331f6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions django/db/models/query.py
@@ -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
Expand Down Expand Up @@ -285,11 +285,14 @@ def get_or_create(self, **kwargs):
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
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
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):
"""
Expand Down

0 comments on commit 1b331f6

Please sign in to comment.