Ticket #16137: get_or_create_no_kwargs.diff

File get_or_create_no_kwargs.diff, 2.1 KB (added by wilfred@…, 13 years ago)

removed assertion, added a simple test

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index ff5289c..73d839b 100644
    a b class QuerySet(object):  
    358358        Returns a tuple of (object, created), where created is a boolean
    359359        specifying whether an object was created.
    360360        """
    361         assert kwargs, \
    362                 'get_or_create() must be passed at least one keyword argument'
    363361        defaults = kwargs.pop('defaults', {})
    364362        lookup = kwargs.copy()
    365363        for f in self.model._meta.fields:
  • tests/modeltests/get_or_create/models.py

    diff --git a/tests/modeltests/get_or_create/models.py b/tests/modeltests/get_or_create/models.py
    index 14f55f8..403b6a9 100644
    a b class Person(models.Model):  
    1616    def __unicode__(self):
    1717        return u'%s %s' % (self.first_name, self.last_name)
    1818
     19class DefaultPerson(models.Model):
     20    first_name = models.CharField(max_length=100, default="Anonymous")
     21
    1922class ManualPrimaryKeyTest(models.Model):
    2023    id = models.IntegerField(primary_key=True)
    2124    data = models.CharField(max_length=100)
  • tests/modeltests/get_or_create/tests.py

    diff --git a/tests/modeltests/get_or_create/tests.py b/tests/modeltests/get_or_create/tests.py
    index 3323c88..c667801 100644
    a b from datetime import date  
    33from django.db import IntegrityError
    44from django.test import TestCase
    55
    6 from models import Person, ManualPrimaryKeyTest
     6from models import Person, DefaultPerson, ManualPrimaryKeyTest
    77
    88
    99class GetOrCreateTests(TestCase):
    class GetOrCreateTests(TestCase):  
    5050            ManualPrimaryKeyTest.objects.get_or_create, id=1, data="Different"
    5151        )
    5252        self.assertEqual(ManualPrimaryKeyTest.objects.get(id=1).data, "Original")
     53
     54    def test_get_or_create_empty(self):
     55        # If all the attributes on a model have defaults, we shouldn't
     56        # need to pass any arguments.
     57        try:
     58            DefaultPerson.objects.get_or_create()
     59        except AssertionError:
     60            self.assertTrue(False)
Back to Top