Ticket #5741: get_with_default.diff

File get_with_default.diff, 2.6 KB (added by Malcolm Tredinnick, 16 years ago)

Updated patch (just the docs bit changed)

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 5b9f504..b05948c 100644
    a b class QuerySet(object):  
    300300        Performs the query and returns a single object matching the given
    301301        keyword arguments.
    302302        """
     303        has_default = False
     304        if 'default' in kwargs:
     305            default_value = kwargs.pop('default')
     306            has_default = True
    303307        clone = self.filter(*args, **kwargs)
    304308        num = len(clone)
    305309        if num == 1:
    306310            return clone._result_cache[0]
    307311        if not num:
     312            if has_default:
     313                return default_value
    308314            raise self.model.DoesNotExist("%s matching query does not exist."
    309315                    % self.model._meta.object_name)
    310316        raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
  • docs/ref/models/querysets.txt

    diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
    index 4a41349..3b756fa 100644
    a b The ``DoesNotExist`` exception inherits from  
    727727    except ObjectDoesNotExist:
    728728        print "Either the entry or blog doesn't exist."
    729729
     730.. versionadded:: development
     731
     732You can optionally pass in a keyword argument called ``default`` to ``get()``.
     733If the object being requested does not exist and a default value is given, the
     734default value will be returned, instead of raising a ``DoesNotExist``
     735exception. For example::
     736
     737    e = Entry.objects.get(id=3, default=None)
     738
     739... would set ``e`` to ``None`` if no such ``Entry`` existed.
     740
     741Since this default argument name might potentially clash with a model field
     742called ``default``, if you wish to filter on a field called ``default``, you
     743should write the filter as ``default__exact``::
     744
     745    SomeModel.objects.get(default__exact=3)
     746
     747This will filter on the ``default`` field, rather than specifying a default
     748return value.
     749
    730750``create(**kwargs)``
    731751~~~~~~~~~~~~~~~~~~~~
    732752
  • tests/modeltests/basic/models.py

    diff --git a/tests/modeltests/basic/models.py b/tests/modeltests/basic/models.py
    index 835c5c9..2cc5d9c 100644
    a b Traceback (most recent call last):  
    100100    ...
    101101DoesNotExist: Article matching query does not exist.
    102102
     103# You can specify a default keyword argument to get()
     104>>> Article.objects.get(pk=97, default=False)
     105False
     106
    103107# Lookup by a primary key is the most common case, so Django provides a
    104108# shortcut for primary-key exact lookups.
    105109# The following is identical to articles.get(id=1).
Back to Top