From 1b331f6c1eadc55e6efe014ec5f5b91df83961ff Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Tue, 18 Mar 2008 14:08:40 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#6641=20--=20If=20we=20lose=20a=20race?= =?UTF-8?q?=20when=20creating=20a=20new=20object=20in=20get=5For=5Fcreate,?= =?UTF-8?q?=20re-get=20the=20result=20from=20the=20database=20and=20return?= =?UTF-8?q?=20that.=20Thanks,=20Jeff=20Guinness=20and=20Timoth=C3=A9e=20Pe?= =?UTF-8?q?ignier.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@7289 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 0bc36f425ad..a3d00c2ead7 100644 --- a/django/db/models/query.py +++ b/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 @@ -285,11 +285,14 @@ class _QuerySet(object): 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): """