diff options
author | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
---|---|---|
committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2006-06-19 15:23:57 +0000 |
commit | adf4b9311d5d64a2bdd58da50271c121ea22e397 (patch) | |
tree | a69b3b023595cf1ce67a14c4c1ecd3290d94088e /django/db/models/query.py | |
parent | e976ed1f7910fad03704f88853c5c5b36cbab134 (diff) | |
download | django-attic/multi-auth.tar.gz |
multi-auth: Merged to [3151]archive/attic/multi-authattic/multi-auth
git-svn-id: http://code.djangoproject.com/svn/django/branches/multi-auth@3152 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/query.py')
-rw-r--r-- | django/db/models/query.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 4bd9b3b9fe..e826efa779 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -205,6 +205,23 @@ class QuerySet(object): assert len(obj_list) == 1, "get() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.model._meta.object_name, len(obj_list), kwargs) return obj_list[0] + def get_or_create(self, **kwargs): + """ + Looks up an object with the given kwargs, creating one if necessary. + Returns a tuple of (object, created), where created is a boolean + specifying whether an object was created. + """ + assert len(kwargs), 'get_or_create() must be passed at least one keyword argument' + defaults = kwargs.pop('defaults', {}) + try: + return self.get(**kwargs), False + except self.model.DoesNotExist: + params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) + params.update(defaults) + obj = self.model(**params) + obj.save() + return obj, True + def latest(self, field_name=None): """ Returns the latest object, according to the model's 'get_latest_by' @@ -529,7 +546,7 @@ class DateQuerySet(QuerySet): c._order = self._order return c -class QOperator: +class QOperator(object): "Base class for QAnd and QOr" def __init__(self, *args): self.args = args |