From b72d8da3b270bd03efe34686b6cc1fcb212ce188 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Tue, 31 Jan 2006 02:04:45 +0000 Subject: [PATCH] magic-removal: removed legacy 'import copy', and re-introduced Q() objects as positional parameters to the QuerySet methods that took keyword arguments git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2198 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/query.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 016cd2a1e3..5636d92975 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1,7 +1,7 @@ from django.db import backend, connection from django.db.models.fields import DateField, FieldDoesNotExist from django.utils.datastructures import SortedDict -import copy +import operator LOOKUP_SEPARATOR = '__' @@ -154,15 +154,15 @@ class QuerySet(object): cursor.execute("SELECT COUNT(*)" + sql, params) return cursor.fetchone()[0] - def get(self, **kwargs): + def get(self, *args, **kwargs): "Performs the SELECT and returns a single object matching the given keyword arguments." - obj_list = list(self.filter(**kwargs)) + obj_list = list(self.filter(*args, **kwargs)) if len(obj_list) < 1: raise self.model.DoesNotExist, "%s does not exist for %s" % (self.model._meta.object_name, kwargs) 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 delete(self, **kwargs): + def delete(self, *args, **kwargs): """ Deletes the records with the given kwargs. If no kwargs are given, deletes records in the current QuerySet. @@ -175,7 +175,7 @@ class QuerySet(object): raise TypeError, "SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data." if kwargs: - del_query = self.filter(**kwargs) + del_query = self.filter(*args, **kwargs) else: del_query = self._clone() # disable non-supported fields @@ -252,11 +252,13 @@ class QuerySet(object): # PUBLIC METHODS THAT RETURN A NEW QUERYSET # ############################################# - def filter(self, **kwargs): + def filter(self, *args, **kwargs): "Returns a new QuerySet instance with the args ANDed to the existing set." clone = self._clone() if len(kwargs) > 0: clone._filters = clone._filters & Q(**kwargs) + if len(args) > 0: + clone._filters = clone._filters & reduce(operator.and_, args) return clone def select_related(self, true_or_false=True):