Ticket #19326: 19326_2.diff

File 19326_2.diff, 2.8 KB (added by wim@…, 12 years ago)

My second take, using filter and IndexError as suggested

  • django/db/models/manager.py

    diff --git a/django/db/models/manager.py b/django/db/models/manager.py
    index 8da8af4..f662131 100644
    a b class Manager(object):  
    139139    def extra(self, *args, **kwargs):
    140140        return self.get_query_set().extra(*args, **kwargs)
    141141
     142    def first(self, *args, **kwargs):
     143        return self.get_query_set().first(*args, **kwargs)
     144
    142145    def get(self, *args, **kwargs):
    143146        return self.get_query_set().get(*args, **kwargs)
    144147
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 130be9a..b4137bf 100644
    a b class QuerySet(object):  
    363363
    364364        return self.query.get_count(using=self.db)
    365365
     366    def first(self, *args, **kwargs):
     367        """
     368        Performs the query and returns the first object matching the given
     369        keyword arguments or None if no match is found.
     370        """
     371        try:
     372            return self.filter(*args, **kwargs)[0]
     373        except IndexError:
     374            return None
     375
    366376    def get(self, *args, **kwargs):
    367377        """
    368378        Performs the query and returns a single object matching the given
  • docs/ref/models/querysets.txt

    diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
    index 295c996..409764e 100644
    a b something *other than* a ``QuerySet``.  
    12381238These methods do not use a cache (see :ref:`caching-and-querysets`). Rather,
    12391239they query the database each time they're called.
    12401240
     1241first
     1242~~~
     1243.. method:: first(**kwargs)
     1244
     1245.. versionadded:: 1.6
     1246
     1247Returns the first of all objects matching the given lookup parameters, which
     1248should be in the format described in `Field lookups`_.
     1249
     1250``first()`` returns None if no object was found for the given parameters.
     1251
     1252``first()`` returns the first object in the QuerySet when multiple objects were
     1253found, as determined by the ordering in the model or in the query.
     1254
     1255Example::
     1256    p = Person.objects.first(first_name="Paul")
     1257
    12411258get
    12421259~~~
    12431260
  • tests/modeltests/basic/tests.py

    diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
    index 1c83b98..c66678c 100644
    a b class ModelTest(TestCase):  
    162162            pub_date__month=7,
    163163        )
    164164
     165        # First should return None when no object is found
     166        self.assertEqual(
     167            Article.objects.first(pub_date__year=2012),
     168            None,
     169        )
     170
     171        # First should return the first object when multiple objects are found
     172        a = Article.objects.filter(pub_date__year=2005)[0]
     173
     174        self.assertEqual(
     175            Article.objects.first(pub_date__year=2005),
     176            a,
     177        )
     178
    165179    def test_object_creation(self):
    166180        # Create an Article.
    167181        a = Article(
Back to Top