Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fixed #16137 - Removed kwargs requirement for QuerySet.get_or_create

Thanks wilfred@, poirier, and charettes for work
on the patch.
  • Loading branch information...
1 parent a64de33 commit 90af278203963e3e3f96e443971cd38a2dad34e4 Tim Graham committed with timgraham
2  django/db/models/
@@ -363,8 +363,6 @@ def get_or_create(self, **kwargs):
Returns a tuple of (object, created), where created is a boolean
specifying whether an object was created.
- assert kwargs, \
- 'get_or_create() must be passed at least one keyword argument'
defaults = kwargs.pop('defaults', {})
lookup = kwargs.copy()
for f in self.model._meta.fields:
8 docs/ref/models/querysets.txt
@@ -1343,8 +1343,12 @@ get_or_create
.. method:: get_or_create(**kwargs)
-A convenience method for looking up an object with the given kwargs, creating
-one if necessary.
+A convenience method for looking up an object with the given kwargs (may be
+empty if your model has defaults for all fields), creating one if necessary.
+.. versionchanged:: 1.6
+ Older versions of Django required ``kwargs``.
Returns a tuple of ``(object, created)``, where ``object`` is the retrieved or
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
* The :exc:`~django.core.exceptions.DoesNotExist` exception now includes a
message indicating the name of the attribute used for the lookup.
+* The :meth:`~django.db.models.query.QuerySet.get_or_create` method no longer
+ requires at least one keyword argument.
Backwards incompatible changes in 1.6
5 tests/get_or_create/
@@ -21,6 +21,11 @@ class Person(models.Model):
def __str__(self):
return '%s %s' % (self.first_name, self.last_name)
+class DefaultPerson(models.Model):
+ first_name = models.CharField(max_length=100, default="Anonymous")
class ManualPrimaryKeyTest(models.Model):
id = models.IntegerField(primary_key=True)
data = models.CharField(max_length=100)
10 tests/get_or_create/
@@ -8,7 +8,7 @@
from django.utils.encoding import DjangoUnicodeDecodeError
from django.test import TestCase, TransactionTestCase
-from .models import Person, ManualPrimaryKeyTest, Profile, Tag, Thing
+from .models import DefaultPerson, Person, ManualPrimaryKeyTest, Profile, Tag, Thing
class GetOrCreateTests(TestCase):
@@ -83,6 +83,14 @@ def test_savepoint_rollback(self):
self.skipTest("This backend accepts broken utf-8.")
+ def test_get_or_create_empty(self):
+ # Regression test for #16137: get_or_create does not require kwargs.
+ try:
+ DefaultPerson.objects.get_or_create()
+ except AssertionError:
+"If all the attributes on a model have defaults, we "
+ "shouldn't need to pass any arguments.")
class GetOrCreateTransactionTests(TransactionTestCase):

0 comments on commit 90af278

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