diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py index a8e9a3f3f9..44e9967501 100644 --- a/tests/get_or_create/models.py +++ b/tests/get_or_create/models.py @@ -9,6 +9,7 @@ class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) birthday = models.DateField() + defaults = models.TextField() def __str__(self): return '%s %s' % (self.first_name, self.last_name) diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index a58e77ec0e..762cf57025 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -126,6 +126,28 @@ class GetOrCreateTests(TestCase): # The publisher should have three books. self.assertEqual(p.books.count(), 3) + def test_defaults_exact(self): + """ + If you have a field named defaults and want to use it as an exact + lookup, you need to use 'defaults__exact'. + """ + obj, created = Person.objects.get_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertTrue(created) + self.assertEqual(obj.defaults, 'testing') + obj2, created = Person.objects.get_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertFalse(created) + self.assertEqual(obj, obj2) + class GetOrCreateTestsWithManualPKs(TestCase): @@ -348,3 +370,25 @@ class UpdateOrCreateTests(TestCase): self.assertFalse(created) self.assertEqual(book.name, name) self.assertEqual(author.books.count(), 1) + + def test_defaults_exact(self): + """ + If you have a field named defaults and want to use it as an exact + lookup, you need to use 'defaults__exact'. + """ + obj, created = Person.objects.update_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'testing', + } + ) + self.assertTrue(created) + self.assertEqual(obj.defaults, 'testing') + obj, created = Person.objects.update_or_create( + first_name='George', last_name='Harrison', defaults__exact='testing', defaults={ + 'birthday': date(1943, 2, 25), + 'defaults': 'another testing', + } + ) + self.assertFalse(created) + self.assertEqual(obj.defaults, 'another testing')