Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16137 - Removed kwargs requirement for QuerySet.get_or_create

Thanks wilfred@, poirier, and charettes for work
on the patch.
  • Loading branch information...
commit 90af278203963e3e3f96e443971cd38a2dad34e4 1 parent a64de33
authored May 24, 2013 timgraham committed May 27, 2013
2  django/db/models/query.py
@@ -363,8 +363,6 @@ def get_or_create(self, **kwargs):
363 363
         Returns a tuple of (object, created), where created is a boolean
364 364
         specifying whether an object was created.
365 365
         """
366  
-        assert kwargs, \
367  
-                'get_or_create() must be passed at least one keyword argument'
368 366
         defaults = kwargs.pop('defaults', {})
369 367
         lookup = kwargs.copy()
370 368
         for f in self.model._meta.fields:
8  docs/ref/models/querysets.txt
@@ -1343,8 +1343,12 @@ get_or_create
1343 1343
 
1344 1344
 .. method:: get_or_create(**kwargs)
1345 1345
 
1346  
-A convenience method for looking up an object with the given kwargs, creating
1347  
-one if necessary.
  1346
+A convenience method for looking up an object with the given kwargs (may be
  1347
+empty if your model has defaults for all fields), creating one if necessary.
  1348
+
  1349
+.. versionchanged:: 1.6
  1350
+
  1351
+    Older versions of Django required ``kwargs``.
1348 1352
 
1349 1353
 Returns a tuple of ``(object, created)``, where ``object`` is the retrieved or
1350 1354
 created object and ``created`` is a boolean specifying whether a new object was
3  docs/releases/1.6.txt
@@ -280,6 +280,9 @@ Minor features
280 280
 * The :exc:`~django.core.exceptions.DoesNotExist` exception now includes a
281 281
   message indicating the name of the attribute used for the lookup.
282 282
 
  283
+* The :meth:`~django.db.models.query.QuerySet.get_or_create` method no longer
  284
+  requires at least one keyword argument.
  285
+
283 286
 Backwards incompatible changes in 1.6
284 287
 =====================================
285 288
 
5  tests/get_or_create/models.py
@@ -21,6 +21,11 @@ class Person(models.Model):
21 21
     def __str__(self):
22 22
         return '%s %s' % (self.first_name, self.last_name)
23 23
 
  24
+
  25
+class DefaultPerson(models.Model):
  26
+    first_name = models.CharField(max_length=100, default="Anonymous")
  27
+
  28
+
24 29
 class ManualPrimaryKeyTest(models.Model):
25 30
     id = models.IntegerField(primary_key=True)
26 31
     data = models.CharField(max_length=100)
10  tests/get_or_create/tests.py
@@ -8,7 +8,7 @@
8 8
 from django.utils.encoding import DjangoUnicodeDecodeError
9 9
 from django.test import TestCase, TransactionTestCase
10 10
 
11  
-from .models import Person, ManualPrimaryKeyTest, Profile, Tag, Thing
  11
+from .models import DefaultPerson, Person, ManualPrimaryKeyTest, Profile, Tag, Thing
12 12
 
13 13
 
14 14
 class GetOrCreateTests(TestCase):
@@ -83,6 +83,14 @@ def test_savepoint_rollback(self):
83 83
         else:
84 84
             self.skipTest("This backend accepts broken utf-8.")
85 85
 
  86
+    def test_get_or_create_empty(self):
  87
+        # Regression test for #16137: get_or_create does not require kwargs.
  88
+        try:
  89
+            DefaultPerson.objects.get_or_create()
  90
+        except AssertionError:
  91
+            self.fail("If all the attributes on a model have defaults, we "
  92
+                      "shouldn't need to pass any arguments.")
  93
+
86 94
 
87 95
 class GetOrCreateTransactionTests(TransactionTestCase):
88 96
 

0 notes on commit 90af278

Please sign in to comment.
Something went wrong with that request. Please try again.