diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 14963ab9c0..9d3dafa26c 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -108,7 +108,6 @@ class ManyRelatedObjectsDescriptor(object): # Dynamically create a class that subclasses the related # model's default manager. manager = types.ClassType('RelatedManager', (self.related.model._default_manager.__class__,), {})() - manager._use_cache = True # Set core_filters on the new manager to limit it to the # foreign-key relationship. diff --git a/django/db/models/manager.py b/django/db/models/manager.py index 9a2f536d8e..4d5910ed16 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -2,8 +2,6 @@ from django.db.models.fields import DateField from django.utils.functional import curry from django.db import backend, connection from django.db.models.query import QuerySet -from django.db.models.query import Q, fill_table_cache, get_cached_row # TODO - remove lots of these -from django.db.models.query import handle_legacy_orderlist, orderlist2sql, orderfield2column from django.dispatch import dispatcher from django.db.models import signals from django.utils.datastructures import SortedDict @@ -23,6 +21,47 @@ def ensure_default_manager(sender): dispatcher.connect(ensure_default_manager, signal=signals.class_prepared) +# class OldSubmittedManager(QuerySet): +# def in_bulk(self, id_list, **kwargs): +# assert isinstance(id_list, list), "in_bulk() must be provided with a list of IDs." +# assert id_list != [], "in_bulk() cannot be passed an empty ID list." +# new_query = self # we have to do a copy later, so this is OK +# if kwargs: +# new_query = self.filter(**kwargs) +# new_query = new_query.extras(where= +# ["%s.%s IN (%s)" % (backend.quote_name(self.klass._meta.db_table), +# backend.quote_name(self.klass._meta.pk.column), +# ",".join(['%s'] * len(id_list)))], +# params=id_list) +# obj_list = list(new_query) +# return dict([(obj._get_pk_val(), obj) for obj in obj_list]) +# +# def delete(self, **kwargs): +# # Remove the DELETE_ALL argument, if it exists. +# delete_all = kwargs.pop('DELETE_ALL', False) +# +# # Check for at least one query argument. +# if not kwargs and not delete_all: +# raise TypeError, "SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data." +# +# if kwargs: +# del_query = self.filter(**kwargs) +# else: +# del_query = self._clone() +# # disable non-supported fields +# del_query._select_related = False +# del_query._select = {} +# del_query._order_by = [] +# del_query._offset = None +# del_query._limit = None +# +# opts = self.klass._meta +# +# # Perform the SQL delete +# cursor = connection.cursor() +# _, sql, params = del_query._get_sql_clause(False) +# cursor.execute("DELETE " + sql, params) + class Manager(QuerySet): # Tracks each time a Manager instance is created. Used to retain order. creation_counter = 0 @@ -34,85 +73,6 @@ class Manager(QuerySet): Manager.creation_counter += 1 self.klass = None - def _prepare(self): - pass - # TODO - #if self.klass._meta.get_latest_by: - # self.get_latest = self.__get_latest - #for f in self.klass._meta.fields: - # if isinstance(f, DateField): - # setattr(self, 'get_%s_list' % f.name, curry(self.__get_date_list, f)) - - def contribute_to_class(self, klass, name): - # TODO: Use weakref because of possible memory leak / circular reference. - self.klass = klass - dispatcher.connect(self._prepare, signal=signals.class_prepared, sender=klass) - setattr(klass, name, ManagerDescriptor(self)) - if not hasattr(klass, '_default_manager') or self.creation_counter < klass._default_manager.creation_counter: - klass._default_manager = self - - def get(self, **kwargs): - """Gets a single object, using a new query. Keyword arguments are filters.""" - obj_list = list(self.filter(**kwargs)) - if len(obj_list) < 1: - raise self.klass.DoesNotExist, "%s does not exist for %s" % (self.klass._meta.object_name, kwargs) - assert len(obj_list) == 1, "get_object() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.klass._meta.object_name, len(obj_list), kwargs) - return obj_list[0] - - def in_bulk(self, id_list, **kwargs): - assert isinstance(id_list, list), "in_bulk() must be provided with a list of IDs." - assert id_list != [], "in_bulk() cannot be passed an empty ID list." - new_query = self # we have to do a copy later, so this is OK - if kwargs: - new_query = self.filter(**kwargs) - new_query = new_query.extras(where= - ["%s.%s IN (%s)" % (backend.quote_name(self.klass._meta.db_table), - backend.quote_name(self.klass._meta.pk.column), - ",".join(['%s'] * len(id_list)))], - params=id_list) - obj_list = list(new_query) - return dict([(obj._get_pk_val(), obj) for obj in obj_list]) - - def delete(self, **kwargs): - # Remove the DELETE_ALL argument, if it exists. - delete_all = kwargs.pop('DELETE_ALL', False) - - # Check for at least one query argument. - if not kwargs and not delete_all: - raise TypeError, "SAFETY MECHANISM: Specify DELETE_ALL=True if you actually want to delete all data." - - if kwargs: - del_query = self.filter(**kwargs) - else: - del_query = self._clone() - # disable non-supported fields - del_query._select_related = False - del_query._select = {} - del_query._order_by = [] - del_query._offset = None - del_query._limit = None - - opts = self.klass._meta - - # Perform the SQL delete - cursor = connection.cursor() - _, sql, params = del_query._get_sql_clause(False) - cursor.execute("DELETE " + sql, params) - - -class OldManager(object): - # Tracks each time a Manager instance is created. Used to retain order. - creation_counter = 0 - - # Dictionary of lookup parameters to apply to every _get_sql_clause(). - core_filters = {} - - def __init__(self): - # Increase the creation counter, and save our local copy. - self.creation_counter = Manager.creation_counter - Manager.creation_counter += 1 - self.klass = None - def _prepare(self): if self.klass._meta.get_latest_by: self.get_latest = self.__get_latest @@ -128,107 +88,6 @@ class OldManager(object): if not hasattr(klass, '_default_manager') or self.creation_counter < klass._default_manager.creation_counter: klass._default_manager = self - def _get_sql_clause(self, allow_joins, *args, **kwargs): - def quote_only_if_word(word): - if ' ' in word: - return word - else: - return backend.quote_name(word) - - opts = self.klass._meta - - # Apply core filters. - kwargs.update(self.core_filters) - - # Construct the fundamental parts of the query: SELECT X FROM Y WHERE Z. - select = ["%s.%s" % (backend.quote_name(opts.db_table), backend.quote_name(f.column)) for f in opts.fields] - tables = (kwargs.get('tables') and [quote_only_if_word(t) for t in kwargs['tables']] or []) - joins = SortedDict() - where = kwargs.get('where') and kwargs['where'][:] or [] - params = kwargs.get('params') and kwargs['params'][:] or [] - - # Convert all the args into SQL. - table_count = 0 - for arg in args: - # check that the provided argument is a Query (i.e., it has a get_sql method) - if not hasattr(arg, 'get_sql'): - raise TypeError, "'%s' is not a valid query argument" % str(arg) - - tables2, joins2, where2, params2 = arg.get_sql(opts) - tables.extend(tables2) - joins.update(joins2) - where.extend(where2) - params.extend(params2) - - # Convert the kwargs into SQL. - tables2, joins2, where2, params2 = parse_lookup(kwargs.items(), opts) - tables.extend(tables2) - joins.update(joins2) - where.extend(where2) - params.extend(params2) - - # Add additional tables and WHERE clauses based on select_related. - if kwargs.get('select_related') is True: - fill_table_cache(opts, select, tables, where, opts.db_table, [opts.db_table]) - - # Add any additional SELECTs passed in via kwargs. - if kwargs.get('select'): - select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in kwargs['select']]) - - # Start composing the body of the SQL statement. - sql = [" FROM", backend.quote_name(opts.db_table)] - - # Check if extra tables are allowed. If not, throw an error - if (tables or joins) and not allow_joins: - raise TypeError, "Joins are not allowed in this type of query" - - # Compose the join dictionary into SQL describing the joins. - if joins: - sql.append(" ".join(["%s %s AS %s ON %s" % (join_type, table, alias, condition) - for (alias, (table, join_type, condition)) in joins.items()])) - - # Compose the tables clause into SQL. - if tables: - sql.append(", " + ", ".join(tables)) - - # Compose the where clause into SQL. - if where: - sql.append(where and "WHERE " + " AND ".join(where)) - - # ORDER BY clause - order_by = [] - for f in handle_legacy_orderlist(kwargs.get('order_by', opts.ordering)): - if f == '?': # Special case. - order_by.append(backend.get_random_function_sql()) - else: - if f.startswith('-'): - col_name = f[1:] - order = "DESC" - else: - col_name = f - order = "ASC" - if "." in col_name: - table_prefix, col_name = col_name.split('.', 1) - table_prefix = backend.quote_name(table_prefix) + '.' - else: - # Use the database table as a column prefix if it wasn't given, - # and if the requested column isn't a custom SELECT. - if "." not in col_name and col_name not in [k[0] for k in kwargs.get('select', [])]: - table_prefix = backend.quote_name(opts.db_table) + '.' - else: - table_prefix = '' - order_by.append('%s%s %s' % (table_prefix, backend.quote_name(orderfield2column(col_name, opts)), order)) - if order_by: - sql.append("ORDER BY " + ", ".join(order_by)) - - # LIMIT and OFFSET clauses - if kwargs.get('limit') is not None: - sql.append("%s " % backend.get_limit_offset_sql(kwargs['limit'], kwargs.get('offset'))) - else: - assert kwargs.get('offset') is None, "'offset' is not allowed without 'limit'" - - return select, " ".join(sql), params - def delete(self, *args, **kwargs): num_args = len(args) + len(kwargs) @@ -253,50 +112,7 @@ class OldManager(object): _, sql, params = self._get_sql_clause(False, *args, **kwargs) cursor.execute("DELETE " + sql, params) - def get_iterator(self, *args, **kwargs): - # kwargs['select'] is a dictionary, and dictionaries' key order is - # undefined, so we convert it to a list of tuples internally. - kwargs['select'] = kwargs.get('select', {}).items() - - cursor = connection.cursor() - select, sql, params = self._get_sql_clause(True, *args, **kwargs) - cursor.execute("SELECT " + (kwargs.get('distinct') and "DISTINCT " or "") + ",".join(select) + sql, params) - fill_cache = kwargs.get('select_related') - index_end = len(self.klass._meta.fields) - while 1: - rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) - if not rows: - raise StopIteration - for row in rows: - if fill_cache: - obj, index_end = get_cached_row(self.klass, row, 0) - else: - obj = self.klass(*row[:index_end]) - for i, k in enumerate(kwargs['select']): - setattr(obj, k[0], row[index_end+i]) - yield obj - - def get_list(self, *args, **kwargs): - return list(self.get_iterator(*args, **kwargs)) - - def get_count(self, *args, **kwargs): - kwargs['order_by'] = [] - kwargs['offset'] = None - kwargs['limit'] = None - kwargs['select_related'] = False - _, sql, params = self._get_sql_clause(True, *args, **kwargs) - cursor = connection.cursor() - cursor.execute("SELECT COUNT(*)" + sql, params) - return cursor.fetchone()[0] - - def get_object(self, *args, **kwargs): - obj_list = self.get_list(*args, **kwargs) - if len(obj_list) < 1: - raise self.klass.DoesNotExist, "%s does not exist for %s" % (self.klass._meta.object_name, kwargs) - assert len(obj_list) == 1, "get_object() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.klass._meta.object_name, len(obj_list), kwargs) - return obj_list[0] - - def get_in_bulk(self, id_list, *args, **kwargs): + def in_bulk(self, id_list, *args, **kwargs): assert isinstance(id_list, list), "get_in_bulk() must be provided with a list of IDs." assert id_list != [], "get_in_bulk() cannot be passed an empty ID list." kwargs['where'] = ["%s.%s IN (%s)" % (backend.quote_name(self.klass._meta.db_table), backend.quote_name(self.klass._meta.pk.column), ",".join(['%s'] * len(id_list)))] @@ -357,9 +173,6 @@ class OldManager(object): # objects -- MySQL returns the values as strings, instead. return [typecast_timestamp(str(row[0])) for row in cursor.fetchall()] -# DEBUG - to go back to old manager: -# Manager = OldManager - class ManagerDescriptor(object): def __init__(self, manager): self.manager = manager diff --git a/django/db/models/query.py b/django/db/models/query.py index cdca7c002f..2ac7b46049 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -51,139 +51,153 @@ def orderlist2sql(order_list, opts, prefix=''): output.append('%s%s ASC' % (prefix, backend.quote_name(orderfield2column(f, opts)))) return ', '.join(output) +def quote_only_if_word(word): + if ' ' in word: + return word + else: + return backend.quote_name(word) + class QuerySet(object): "Represents a lazy database lookup for a set of objects" - # Sub classes need to provide 'opts' member for this class + # Subclasses need to provide 'self.klass' attribute for this class # to be able to function. + + # Dictionary of lookup parameters to apply to every _get_sql_clause(). + core_filters = {} + def __init__(self): - self._filter = Q() - self._order_by = () - self._select_related = False - self._distinct = True - self._result_cache = None - self._params = None - self._select = None - self._where = None - self._tables = None - self._offset = None - self._limit = None - self._use_cache = False + self._filters = {} # Dictionary of lookup parameters, e.g. {'foo__gt': 3} + self._order_by = () # Ordering, e.g. ('date', '-name') + self._select_related = False # Whether to fill cache for related objects. + self._distinct = False # Whether the query should use SELECT DISTINCT. +# self._result_cache = None + self._select = None # Dictionary of attname -> SQL. + self._where = None # List of extra WHERE clauses to use. + self._params = None # List of params to use for extra WHERE clauses. + self._tables = None # List of extra tables to use. + self._offset = None # OFFSET clause + self._limit = None # LIMIT clause +# self._use_cache = False - def filter(self, **kwargs): - """Returns a new query instance with the query arguments - ANDed to the existing set""" - clone = self._clone() - clone._filter = self._filter & Q(**kwargs) - return clone + ######################## + # PYTHON MAGIC METHODS # + ######################## - def unique(self, true_or_false): - """Returns a new query instance with the 'unique' qualifier modified""" - return self._clone(_distinct=true_or_false) +# def __len__(self): +# return len(list(self)) - def order_by(self, *field_names): - """Returns a new query instance with the ordering changed.""" - return self._clone(_order_by=field_names) + ########################################### + # PUBLIC METHODS THAT DO DATABASE QUERIES # + ########################################### - def select_related(self, true_or_false): - """Returns a new query instance with the 'related' qualifier modified""" - return self._clone(_related=true_or_false) + def __iter__(self): + "Performs the SELECT database lookup of this QuerySet." + # self._select is a dictionary, and dictionaries' key order is + # undefined, so we convert it to a list of tuples. + extra_select = (self._select or {}).items() + + cursor = connection.cursor() + select, sql, params = self._get_sql_clause(True) + cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) + fill_cache = self._select_related + index_end = len(self.klass._meta.fields) + while 1: + rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) + if not rows: + raise StopIteration + for row in rows: + if fill_cache: + obj, index_end = get_cached_row(self.klass, row, 0) + else: + obj = self.klass(*row[:index_end]) + for i, k in enumerate(extra_select): + setattr(obj, k[0], row[index_end+i]) + yield obj def count(self): + "Performs a SELECT COUNT() and returns the number of records as an integer." counter = self._clone() counter._order_by = [] - - # TODO - do we change these or not? - # e.g. if someone does objects[0:10].count() - # (which - #counter._offset = None - #counter._limit = None + counter._offset = None + counter._limit = None counter._select_related = False _, sql, params = counter._get_sql_clause(True) cursor = connection.cursor() cursor.execute("SELECT COUNT(*)" + sql, params) return cursor.fetchone()[0] - # Convenience function for subclasses - def _set_core_filter(self, **kwargs): - """Sets the filters that should always be applied to queries""" - self._filter = Q(**kwargs) + def get(self, **kwargs): + "Performs the SELECT and returns a single object matching the given keyword arguments." + obj_list = list(self.filter(**kwargs)) + if len(obj_list) < 1: + raise self.klass.DoesNotExist, "%s does not exist for %s" % (self.klass._meta.object_name, kwargs) + assert len(obj_list) == 1, "get() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.klass._meta.object_name, len(obj_list), kwargs) + return obj_list[0] - def _clone(self, **kwargs): - """Gets a clone of the object, with optional kwargs to alter the clone""" - # Don't clone (even temporarily) the cache - _result_cache_save = self._result_cache - self._result_cache = None - # Must ensure we get fully deep copies of all the query objects - clone = copy.deepcopy(self) - # apply changes to clone - clone.__dict__.update(kwargs) - # restore cache - self._result_cache = _result_cache_save + ############################################# + # PUBLIC METHODS THAT RETURN A NEW QUERYSET # + ############################################# + + def filter(self, **kwargs): + "Returns a new QuerySet instance with the args ANDed to the existing set." + clone = self._clone() + clone._filters.update(**kwargs) return clone - def _ensure_compatible(self, other): - if self._distinct != other._distinct: - raise ValueException, "Can't combine a unique query with a non-unique query" + def select_related(self, true_or_false=True): + "Returns a new QuerySet instance with '_select_related' modified." + return self._clone(_select_related=true_or_false) - def _combine(self, other): - self._ensure_compatible(other) - # get a deepcopy of 'other's order by - # (so that A.filter(args1) & A.filter(args2) does the same as - # A.filter(args1).filter(args2) - combined = other._clone() - # If 'self' is ordered and 'other' isn't, propagate 'self's ordering - if len(self._order_by) > 0 and len(combined._order_by == 0): - combined._order_by = copy.deepcopy(self._order_by) - return combined + def order_by(self, *field_names): + "Returns a new QuerySet instance with the ordering changed." + return self._clone(_order_by=field_names) - def extras(self, params=None, select=None, where=None, tables=None): - return self._clone(_params=params, _select=select, _where=where, _tables=tables) + def distinct(self, true_or_false=True): + "Returns a new QuerySet instance with '_distinct' modified." + return self._clone(_distinct=true_or_false) - def __and__(self, other): - combined = self._combine(other) - combined._filter = self._filter & other._filter - return combined + ################### + # PRIVATE METHODS # + ################### - def __or__(self, other): - combined = self._combine(other) - combined._filter = self._filter | other._filter - return combined + def _clone(self, **kwargs): + c = QuerySet() + c._filters = self._filters.copy() + c._order_by = self._order_by + c._select_related = self._select_related + c._distinct = self._distinct + c._select = self._select + c._offset = self._offset + c._limit = self._limit + return c - # TODO - allow_joins - do we need it? def _get_sql_clause(self, allow_joins): - def quote_only_if_word(word): - if ' ' in word: - return word - else: - return backend.quote_name(word) - - # This is defined by sub-classes - # TODO - define a better accessor opts = self.klass._meta + # Apply core filters. + self._filters.update(self.core_filters) + # Construct the fundamental parts of the query: SELECT X FROM Y WHERE Z. select = ["%s.%s" % (backend.quote_name(opts.db_table), backend.quote_name(f.column)) for f in opts.fields] - tables = [quote_only_if_word(t) for t in (self._tables or [])] joins = SortedDict() where = self._where or [] params = self._params or [] - # Convert the Q object into SQL. - tables2, joins2, where2, params2 = self._filter.get_sql(opts) - + # Convert self._filters into SQL. + tables2, joins2, where2, params2 = parse_lookup(self._filters.items(), opts) tables.extend(tables2) joins.update(joins2) where.extend(where2) params.extend(params2) # Add additional tables and WHERE clauses based on select_related. - if self._select_related is True: + if self._select_related: fill_table_cache(opts, select, tables, where, opts.db_table, [opts.db_table]) # Add any additional SELECTs. if self._select: - select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select ]) + select.extend(['(%s) AS %s' % (quote_only_if_word(s[1]), backend.quote_name(s[0])) for s in self._select]) # Start composing the body of the SQL statement. sql = [" FROM", backend.quote_name(opts.db_table)] @@ -207,7 +221,7 @@ class QuerySet(object): # ORDER BY clause order_by = [] - for f in handle_legacy_orderlist(self._order_by): + for f in handle_legacy_orderlist(self._order_by or opts.ordering): if f == '?': # Special case. order_by.append(backend.get_random_function_sql()) else: @@ -223,7 +237,7 @@ class QuerySet(object): else: # Use the database table as a column prefix if it wasn't given, # and if the requested column isn't a custom SELECT. - if "." not in col_name and col_name not in [k[0] for k in (self._select or []) ]: + if "." not in col_name and col_name not in [k[0] for k in (self._select or ())]: table_prefix = backend.quote_name(opts.db_table) + '.' else: table_prefix = '' @@ -239,80 +253,72 @@ class QuerySet(object): return select, " ".join(sql), params - def _fetch_data(self): - if self._use_cache: - if self._result_cache is None: - self._result_cache = list(self.get_iterator()) - return self._result_cache - else: - return list(self.get_iterator()) - - def __iter__(self): - """Gets an iterator for the data""" - # Fetch the data or use get_iterator? If not, we can't - # do sequence operations - or doing so will require re-fetching - # Also, lots of things in current template system break if we - # don't get it all. - return iter(self._fetch_data()) - - def __len__(self): - return len(self._fetch_data()) - - def __getitem__(self, k): - """Retrieve an item or slice from the set of results""" - # getitem can't return query instances, because .filter() - # and .order_by() methods on the result would break badly. - # This means we don't have to worry about arithmetic with - # self._limit or self._offset - they will both be None - # at this point - if isinstance(k, slice): - # Get a new query if we haven't already got data from db - if self._result_cache is None: - # slice.stop and slice.start - clone = self._clone(_offset=k.start, _limit=k.stop) - return list(clone)[::k.step] - # TODO - we are throwing away this retrieved data. - # We could cache it if we had some kind of sparse - # list structure we could put it in. - else: - return self._result_cache[k] - - else: - # TODO: possibly use a new query which just gets one item - # if we haven't already got them all? - return self._fetch_data()[k] - - def get_iterator(self): - # self._select is a dictionary, and dictionaries' key order is - # undefined, so we convert it to a list of tuples. - _extra_select = (self._select or {}).items() - - cursor = connection.cursor() - select, sql, params = self._get_sql_clause(True) - cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) - fill_cache = self._select_related - index_end = len(self.klass._meta.fields) - while 1: - rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE) - if not rows: - raise StopIteration - for row in rows: - if fill_cache: - obj, index_end = get_cached_row(self.klass, row, 0) - else: - obj = self.klass(*row[:index_end]) - for i, k in enumerate(_extra_select): - setattr(obj, k[0], row[index_end+i]) - yield obj +# class QuerySet(object): +# def _ensure_compatible(self, other): +# if self._distinct != other._distinct: +# raise ValueException, "Can't combine a unique query with a non-unique query" +# +# def _combine(self, other): +# self._ensure_compatible(other) +# # get a deepcopy of 'other's order by +# # (so that A.filter(args1) & A.filter(args2) does the same as +# # A.filter(args1).filter(args2) +# combined = other._clone() +# # If 'self' is ordered and 'other' isn't, propagate 'self's ordering +# if len(self._order_by) > 0 and len(combined._order_by == 0): +# combined._order_by = copy.deepcopy(self._order_by) +# return combined +# +# def extras(self, params=None, select=None, where=None, tables=None): +# return self._clone(_params=params, _select=select, _where=where, _tables=tables) +# +# def __and__(self, other): +# combined = self._combine(other) +# combined._filter = self._filter & other._filter +# return combined +# +# def __or__(self, other): +# combined = self._combine(other) +# combined._filter = self._filter | other._filter +# return combined +# +# def _fetch_data(self): +# if self._use_cache: +# if self._result_cache is None: +# self._result_cache = list(self.get_iterator()) +# return self._result_cache +# else: +# return list(self.get_iterator()) +# +# def __getitem__(self, k): +# """Retrieve an item or slice from the set of results""" +# # getitem can't return query instances, because .filter() +# # and .order_by() methods on the result would break badly. +# # This means we don't have to worry about arithmetic with +# # self._limit or self._offset - they will both be None +# # at this point +# if isinstance(k, slice): +# # Get a new query if we haven't already got data from db +# if self._result_cache is None: +# # slice.stop and slice.start +# clone = self._clone(_offset=k.start, _limit=k.stop) +# return list(clone)[::k.step] +# # TODO - we are throwing away this retrieved data. +# # We could cache it if we had some kind of sparse +# # list structure we could put it in. +# else: +# return self._result_cache[k] +# +# else: +# # TODO: possibly use a new query which just gets one item +# # if we haven't already got them all? +# return self._fetch_data()[k] class QOperator: "Base class for QAnd and QOr" def __init__(self, *args): self.args = args - def __repr__(self): - return '(%s)' % self.operator.join([repr(el) for el in self.args]) - def get_sql(self, opts): tables, joins, where, params = [], {}, [], [] for val in self.args: @@ -327,10 +333,7 @@ class QAnd(QOperator): "Encapsulates a combined query that uses 'AND'." operator = ' AND ' def __or__(self, other): - if isinstance(other, (QAnd, QOr, Q)): - return QOr(self, other) - else: - raise TypeError, other + return QOr(self, other) def __and__(self, other): if isinstance(other, QAnd): @@ -344,10 +347,7 @@ class QOr(QOperator): "Encapsulates a combined query that uses 'OR'." operator = ' OR ' def __and__(self, other): - if isinstance(other, (QAnd, QOr, Q)): - return QAnd(self, other) - else: - raise TypeError, other + return QAnd(self, other) def __or__(self, other): if isinstance(other, QOr): @@ -362,20 +362,11 @@ class Q: def __init__(self, **kwargs): self.kwargs = kwargs - def __repr__(self): - return 'Q%r' % self.kwargs - def __and__(self, other): - if isinstance(other, (Q, QAnd, QOr)): - return QAnd(self, other) - else: - raise TypeError, other + return QAnd(self, other) def __or__(self, other): - if isinstance(other, (Q, QAnd, QOr)): - return QOr(self, other) - else: - raise TypeError, other + return QOr(self, other) def get_sql(self, opts): return parse_lookup(self.kwargs.items(), opts) @@ -493,7 +484,6 @@ def find_field(name, field_list, use_accessor=False): Finds a field with a specific name in a list of field instances. Returns None if there are no matches, or several matches. """ - if use_accessor: matches = [f for f in field_list if f.OLD_get_accessor_name() == name] else: