Skip to content

Loading…

Fixed #16137 - Removed kwargs requirement for QuerySet.get_or_create #1214

Closed
wants to merge 1 commit into from

2 participants

@timgraham
Django member

Thanks wilfred@, poirier, and charettes for work
on the patch.

Tim Graham Fixed #16137 - Removed kwargs requirement for QuerySet.get_or_create
Thanks wilfred@, poirier, and charettes for work
on the patch.
989e3d8
@aaugustin aaugustin commented on the diff
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 verions of Django required ``kwargs``.
@aaugustin Django member

typo: versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@aaugustin aaugustin commented on the diff
tests/get_or_create/tests.py
@@ -82,6 +82,13 @@ def test_savepoint_rollback(self):
else:
self.skipTest("This backend accepts broken utf-8.")
+ def test_get_or_create_empty(self):
@aaugustin Django member

You could add a comment:

# Regression test for #16137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 24, 2013
  1. Fixed #16137 - Removed kwargs requirement for QuerySet.get_or_create

    Tim Graham committed
    Thanks wilfred@, poirier, and charettes for work
    on the patch.
View
2 django/db/models/query.py
@@ -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:
View
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 verions of Django required ``kwargs``.
@aaugustin Django member

typo: versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
View
3 docs/releases/1.6.txt
@@ -266,6 +266,9 @@ Minor features
``False`` allows the field to reference proxy models. The default is ``True``
to retain the old behavior.
+* 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
=====================================
View
5 tests/get_or_create/models.py
@@ -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)
View
9 tests/get_or_create/tests.py
@@ -7,7 +7,7 @@
from django.db import IntegrityError, DatabaseError
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):
@@ -82,6 +82,13 @@ def test_savepoint_rollback(self):
else:
self.skipTest("This backend accepts broken utf-8.")
+ def test_get_or_create_empty(self):
@aaugustin Django member

You could add a comment:

# Regression test for #16137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ try:
+ DefaultPerson.objects.get_or_create()
+ except AssertionError:
+ self.fail("If all the attributes on a model have defaults, we "
+ "shouldn't need to pass any arguments.")
+
class GetOrCreateTransactionTests(TransactionTestCase):
Something went wrong with that request. Please try again.