From 758d99697f94a9f210e22a38c96c511d7ee0b7a2 Mon Sep 17 00:00:00 2001
From: Jonas Obrist <ojiidotch@gmail.com>
Date: Sat, 24 Dec 2011 23:29:38 +0100
Subject: [PATCH] updated patch 3 of 16340 to trunk
---
django/db/models/query.py | 4 +++-
tests/modeltests/get_or_create/tests.py | 13 +++++++++++++
2 files changed, 16 insertions(+), 1 deletions(-)
diff --git a/django/db/models/query.py b/django/db/models/query.py
index fd531dd..e70e431 100644
a
|
b
|
|
4 | 4 | |
5 | 5 | import copy |
6 | 6 | import itertools |
| 7 | import sys |
7 | 8 | |
8 | 9 | from django.db import connections, router, transaction, IntegrityError |
9 | 10 | from django.db.models.fields import AutoField |
… |
… |
def get_or_create(self, **kwargs):
|
450 | 451 | return obj, True |
451 | 452 | except IntegrityError, e: |
452 | 453 | transaction.savepoint_rollback(sid, using=self.db) |
| 454 | exc_info = sys.exc_info() |
453 | 455 | try: |
454 | 456 | return self.get(**lookup), False |
455 | 457 | except self.model.DoesNotExist: |
456 | | raise e |
| 458 | raise exc_info[1], None, exc_info[2] |
457 | 459 | |
458 | 460 | def latest(self, field_name=None): |
459 | 461 | """ |
diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
index 4cf4450..64dfdbf 100644
a
|
b
|
|
1 | 1 | from __future__ import absolute_import |
2 | 2 | |
3 | 3 | from datetime import date |
| 4 | import sys |
| 5 | import traceback |
4 | 6 | |
5 | 7 | from django.db import IntegrityError |
6 | 8 | from django.test import TestCase |
… |
… |
def test_get_or_create(self):
|
52 | 54 | ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different" |
53 | 55 | ) |
54 | 56 | self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original") |
| 57 | |
| 58 | # get_or_create should raise IntegrityErrors with the full traceback. |
| 59 | # This is tested by checking that a known method call is in the traceback. |
| 60 | # We cannot use assertRaises/assertRaises here because we need to inspect |
| 61 | # the actual traceback. |
| 62 | try: |
| 63 | ManualPrimaryKeyTest.objects.get_or_create(id=1, data="Different") |
| 64 | except IntegrityError, e: |
| 65 | formatted_traceback = traceback.format_exc() |
| 66 | self.assertIn('obj.save', formatted_traceback) |
| 67 | |