Code

Ticket #16137: 16137.2.diff

File 16137.2.diff, 2.6 KB (added by charettes, 2 years ago)

With a more descriptive test failure

Line 
1diff --git a/django/db/models/query.py b/django/db/models/query.py
2index be42d02..48e911b 100644
3--- a/django/db/models/query.py
4+++ b/django/db/models/query.py
5@@ -414,8 +414,6 @@ class QuerySet(object):
6         Returns a tuple of (object, created), where created is a boolean
7         specifying whether an object was created.
8         """
9-        assert kwargs, \
10-                'get_or_create() must be passed at least one keyword argument'
11         defaults = kwargs.pop('defaults', {})
12         lookup = kwargs.copy()
13         for f in self.model._meta.fields:
14diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
15index 49284fb..b2a8889 100644
16--- a/docs/releases/1.4.txt
17+++ b/docs/releases/1.4.txt
18@@ -480,6 +480,9 @@ Django 1.4 also includes several smaller improvements worth noting:
19 * Added the :djadminopt:`--no-location` option to the :djadmin:`makemessages`
20   command.
21 
22+* The :meth:`~django.db.models.query.QuerySet.get_or_create` method no longer
23+  requires at least one keyword argument.
24+
25 .. _backwards-incompatible-changes-1.4:
26 
27 Backwards incompatible changes in 1.4
28diff --git a/tests/modeltests/get_or_create/models.py b/tests/modeltests/get_or_create/models.py
29index 1de5a6e..d97c6c2 100644
30--- a/tests/modeltests/get_or_create/models.py
31+++ b/tests/modeltests/get_or_create/models.py
32@@ -17,6 +17,11 @@ class Person(models.Model):
33     def __unicode__(self):
34         return u'%s %s' % (self.first_name, self.last_name)
35 
36+
37+class DefaultPerson(models.Model):
38+    first_name = models.CharField(max_length=100, default="Anonymous")
39+
40+
41 class ManualPrimaryKeyTest(models.Model):
42     id = models.IntegerField(primary_key=True)
43     data = models.CharField(max_length=100)
44diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
45index 4cf4450..47c7fef 100644
46--- a/tests/modeltests/get_or_create/tests.py
47+++ b/tests/modeltests/get_or_create/tests.py
48@@ -5,7 +5,7 @@ from datetime import date
49 from django.db import IntegrityError
50 from django.test import TestCase
51 
52-from .models import Person, ManualPrimaryKeyTest
53+from .models import Person, DefaultPerson, ManualPrimaryKeyTest
54 
55 
56 class GetOrCreateTests(TestCase):
57@@ -52,3 +52,10 @@ class GetOrCreateTests(TestCase):
58             ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
59         )
60         self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
61+
62+    def test_get_or_create_empty(self):
63+        try:
64+            DefaultPerson.objects.get_or_create()
65+        except AssertionError:
66+            self.fail("If all the attributes on a model have defaults, we "
67+                      "shouldn't need to pass any arguments.")