desc stringlengths 3 26.7k | decl stringlengths 11 7.89k | bodies stringlengths 8 553k |
|---|---|---|
'Returns True if the QuerySet is ordered -- i.e. has an order_by()
clause or a default ordering on the model.'
| def ordered(self):
| if (self.query.extra_order_by or self.query.order_by):
return True
elif (self.query.default_ordering and self.query.model._meta.ordering):
return True
else:
return False
|
'Return the database that will be used if this query is executed now'
| @property
def db(self):
| if self._for_write:
return (self._db or router.db_for_write(self.model))
return (self._db or router.db_for_read(self.model))
|
'A little helper method for bulk_insert to insert the bulk one batch
at a time. Inserts recursively a batch from the front of the bulk and
then _batched_insert() the remaining objects again.'
| def _batched_insert(self, objs, fields, batch_size):
| if (not objs):
return
ops = connections[self.db].ops
batch_size = (batch_size or max(ops.bulk_batch_size(fields, objs), 1))
for batch in [objs[i:(i + batch_size)] for i in range(0, len(objs), batch_size)]:
self.model._base_manager._insert(batch, fields=fields, using=self.db)
|
'Fills the result cache with \'num\' more entries (or until the results
iterator is exhausted).'
| def _fill_cache(self, num=None):
| if self._iter:
try:
for i in range((num or ITER_CHUNK_SIZE)):
self._result_cache.append(next(self._iter))
except StopIteration:
self._iter = None
|
'Indicates that the next filter call and the one following that should
be treated as a single filter. This is only important when it comes to
determining when to reuse tables for many-to-many filters. Required so
that we can filter naturally on the results of related managers.
This doesn\'t return a clone of the curren... | def _next_is_sticky(self):
| self._sticky_filter = True
return self
|
'Checks that we are merging two comparable QuerySet classes. By default
this does nothing, but see the ValuesQuerySet for an example of where
it\'s useful.'
| def _merge_sanity_check(self, other):
| pass
|
'Keep track of all known related objects from either QuerySet instance.'
| def _merge_known_related_objects(self, other):
| for (field, objects) in other._known_related_objects.items():
self._known_related_objects.setdefault(field, {}).update(objects)
|
'Prepare the query for computing a result that contains aggregate annotations.'
| def _setup_aggregate_query(self, aggregates):
| opts = self.model._meta
if (self.query.group_by is None):
field_names = [f.attname for f in opts.fields]
self.query.add_fields(field_names, False)
self.query.set_group_by()
|
'Returns the internal query\'s SQL and parameters (as a tuple).'
| def _as_sql(self, connection):
| obj = self.values('pk')
if ((obj._db is None) or (connection == connections[obj._db])):
return obj.query.get_compiler(connection=connection).as_nested_sql()
raise ValueError("Can't do subqueries with queries on different DBs.")
|
'Constructs the field_names list that the values query will be
retrieving.
Called by the _clone() method after initializing the rest of the
instance.'
| def _setup_query(self):
| self.query.clear_deferred_loading()
self.query.clear_select_fields()
if self._fields:
self.extra_names = []
self.aggregate_names = []
if ((not self.query.extra) and (not self.query.aggregates)):
self.field_names = list(self._fields)
else:
self.query.de... |
'Cloning a ValuesQuerySet preserves the current fields.'
| def _clone(self, klass=None, setup=False, **kwargs):
| c = super(ValuesQuerySet, self)._clone(klass, **kwargs)
if (not hasattr(c, '_fields')):
c._fields = self._fields[:]
c.field_names = self.field_names
c.extra_names = self.extra_names
c.aggregate_names = self.aggregate_names
if (setup and hasattr(c, '_setup_query')):
c._setup_query... |
'Prepare the query for computing a result that contains aggregate annotations.'
| def _setup_aggregate_query(self, aggregates):
| self.query.set_group_by()
if (self.aggregate_names is not None):
self.aggregate_names.extend(aggregates)
self.query.set_aggregate_mask(self.aggregate_names)
super(ValuesQuerySet, self)._setup_aggregate_query(aggregates)
|
'For ValueQuerySet (and subclasses like ValuesListQuerySet), they can
only be used as nested queries if they\'re already set up to select only
a single field (in which case, that is the field column that is
returned). This differs from QuerySet.as_sql(), where the column to
select is set up by Django.'
| def _as_sql(self, connection):
| if ((self._fields and (len(self._fields) > 1)) or ((not self._fields) and (len(self.model._meta.fields) > 1))):
raise TypeError(('Cannot use a multi-field %s as a filter value.' % self.__class__.__name__))
obj = self._clone()
if ((obj._db is None) or (connection == connection... |
'Validates that we aren\'t trying to do a query like
value__in=qs.values(\'value1\', \'value2\'), which isn\'t valid.'
| def _prepare(self):
| if ((self._fields and (len(self._fields) > 1)) or ((not self._fields) and (len(self.model._meta.fields) > 1))):
raise TypeError(('Cannot use a multi-field %s as a filter value.' % self.__class__.__name__))
return self
|
'Sets up any special features of the query attribute.
Called by the _clone() method after initializing the rest of the
instance.'
| def _setup_query(self):
| self.query.clear_deferred_loading()
self.query = self.query.clone(klass=sql.DateQuery, setup=True)
self.query.select = []
self.query.add_date_select(self._field_name, self._kind, self._order)
|
'Always returns EmptyQuerySet.'
| def all(self):
| return self
|
'Always returns EmptyQuerySet.'
| def filter(self, *args, **kwargs):
| return self
|
'Always returns EmptyQuerySet.'
| def exclude(self, *args, **kwargs):
| return self
|
'Always returns EmptyQuerySet.'
| def complex_filter(self, filter_obj):
| return self
|
'Always returns EmptyQuerySet.'
| def select_related(self, *fields, **kwargs):
| return self
|
'Always returns EmptyQuerySet.'
| def annotate(self, *args, **kwargs):
| return self
|
'Always returns EmptyQuerySet.'
| def order_by(self, *field_names):
| return self
|
'Always returns EmptyQuerySet.'
| def distinct(self, *field_names):
| return self
|
'Always returns EmptyQuerySet.'
| def extra(self, select=None, where=None, params=None, tables=None, order_by=None, select_params=None):
| assert self.query.can_filter(), 'Cannot change a query once a slice has been taken'
return self
|
'Always returns EmptyQuerySet.'
| def reverse(self):
| return self
|
'Always returns EmptyQuerySet.'
| def defer(self, *fields):
| return self
|
'Always returns EmptyQuerySet.'
| def only(self, *fields):
| return self
|
'Don\'t update anything.'
| def update(self, **kwargs):
| return 0
|
'Return a dict mapping the aggregate names to None'
| def aggregate(self, *args, **kwargs):
| for arg in args:
kwargs[arg.default_alias] = arg
return dict([(key, None) for key in kwargs])
|
'Always returns EmptyQuerySet.'
| def values(self, *fields):
| return self
|
'Always returns EmptyQuerySet.'
| def values_list(self, *fields, **kwargs):
| return self
|
'Return the database that will be used if this query is executed now'
| @property
def db(self):
| return (self._db or router.db_for_read(self.model))
|
'Selects which database this Raw QuerySet should excecute it\'s query against.'
| def using(self, alias):
| return RawQuerySet(self.raw_query, model=self.model, query=self.query.clone(using=alias), params=self.params, translations=self.translations, using=alias)
|
'A list of model field names in the order they\'ll appear in the
query results.'
| @property
def columns(self):
| if (not hasattr(self, '_columns')):
self._columns = self.query.get_columns()
for (query_name, model_name) in self.translations.items():
try:
index = self._columns.index(query_name)
self._columns[index] = model_name
except ValueError:
... |
'A dict mapping column names to model field names.'
| @property
def model_fields(self):
| if (not hasattr(self, '_model_fields')):
converter = connections[self.db].introspection.table_name_converter
self._model_fields = {}
for field in self.model._meta.fields:
(name, column) = field.get_attname_column()
self._model_fields[converter(column)] = field
ret... |
'Instantiate a new aggregate.
* lookup is the field on which the aggregate operates.
* extra is a dictionary of additional data to provide for the
aggregate definition
Also utilizes the class variables:
* name, the identifier for this aggregate function.'
| def __init__(self, lookup, **extra):
| self.lookup = lookup
self.extra = extra
|
'Add the aggregate to the nominated query.
This method is used to convert the generic Aggregate definition into a
backend-specific definition.
* query is the backend-specific query instance to which the aggregate
is to be added.
* col is a column reference describing the subject field
of the aggregate. It can be an ali... | def add_to_query(self, query, alias, col, source, is_summary):
| klass = getattr(query.aggregates_module, self.name)
aggregate = klass(col, source=source, is_summary=is_summary, **self.extra)
query.aggregates[alias] = aggregate
|
'Retrieves and caches the value from the datastore on the first lookup.
Returns the cached value.'
| def __get__(self, instance, owner):
| from django.db.models.fields import FieldDoesNotExist
non_deferred_model = instance._meta.proxy_for_model
opts = non_deferred_model._meta
assert (instance is not None)
data = instance.__dict__
if (data.get(self.field_name, self) is self):
try:
f = opts.get_field_by_name(self.... |
'Deferred loading attributes can be set normally (which means there will
never be a database lookup involved.'
| def __set__(self, instance, value):
| instance.__dict__[self.field_name] = value
|
'Check if the field value can be fetched from a parent field already
loaded in the instance. This can be done if the to-be fetched
field is a primary key field.'
| def _check_parent_chain(self, instance, name):
| opts = instance._meta
f = opts.get_field_by_name(name)[0]
link_field = opts.get_ancestor_link(f.model)
if (f.primary_key and (f != link_field)):
return getattr(instance, link_field.attname)
return None
|
'Sets the creation counter value for this instance and increments the
class-level copy.'
| def _set_creation_counter(self):
| self.creation_counter = Manager.creation_counter
Manager.creation_counter += 1
|
'Makes a copy of the manager and assigns it to \'model\', which should be
a child of the existing model (used when inheriting a manager from an
abstract base class).'
| def _copy_to_model(self, model):
| assert issubclass(model, self.model)
mgr = copy.copy(self)
mgr._set_creation_counter()
mgr.model = model
mgr._inherited = True
return mgr
|
'Returns a new QuerySet object. Subclasses can override this method
to easily customize the behavior of the Manager.'
| def get_query_set(self):
| return QuerySet(self.model, using=self._db)
|
'Returns the index of the primary key field in the self.fields list.'
| def pk_index(self):
| return self.fields.index(self.pk)
|
'Does the internal setup so that the current model is a proxy for
"target".'
| def setup_proxy(self, target):
| self.pk = target._meta.pk
self.proxy_for_model = target
self.db_table = target._meta.db_table
|
'There are a few places where the untranslated verbose name is needed
(so that we get the same value regardless of currently active
locale).'
| def verbose_name_raw(self):
| lang = get_language()
deactivate_all()
raw = force_text(self.verbose_name)
activate(lang)
return raw
|
'Has this model been swapped out for another? If so, return the model
name of the replacement; otherwise, return None.
For historical reasons, model name lookups using get_model() are
case insensitive, so we make sure we are case insensitive here.'
| def _swapped(self):
| if self.swappable:
model_label = (u'%s.%s' % (self.app_label, self.object_name.lower()))
swapped_for = getattr(settings, self.swappable, None)
if swapped_for:
try:
(swapped_label, swapped_object) = swapped_for.split(u'.')
except ValueError:
... |
'The getter for self.fields. This returns the list of field objects
available to this model (including through parent models).
Callers are not permitted to modify this list, since it\'s a reference
to this instance (not a copy).'
| def _fields(self):
| try:
self._field_name_cache
except AttributeError:
self._fill_fields_cache()
return self._field_name_cache
|
'Returns a sequence of (field, model) pairs for all fields. The "model"
element is None for fields on the current model. Mostly of use when
constructing queries so that we know which model a field belongs to.'
| def get_fields_with_model(self):
| try:
self._field_cache
except AttributeError:
self._fill_fields_cache()
return self._field_cache
|
'The many-to-many version of get_fields_with_model().'
| def get_m2m_with_model(self):
| try:
self._m2m_cache
except AttributeError:
self._fill_m2m_cache()
return list(six.iteritems(self._m2m_cache))
|
'Returns the requested field by name. Raises FieldDoesNotExist on error.'
| def get_field(self, name, many_to_many=True):
| to_search = ((many_to_many and (self.fields + self.many_to_many)) or self.fields)
for f in to_search:
if (f.name == name):
return f
raise FieldDoesNotExist((u'%s has no field named %r' % (self.object_name, name)))
|
'Returns the (field_object, model, direct, m2m), where field_object is
the Field instance for the given name, model is the model containing
this field (None for local fields), direct is True if the field exists
on this model, and m2m is True for many-to-many relations. When
\'direct\' is False, \'field_object\' is the ... | def get_field_by_name(self, name):
| try:
try:
return self._name_map[name]
except AttributeError:
cache = self.init_name_map()
return cache[name]
except KeyError:
raise FieldDoesNotExist((u'%s has no field named %r' % (self.object_name, name)))
|
'Returns a list of all field names that are possible for this model
(including reverse relation names). This is used for pretty printing
debugging output (a list of choices), so any internal-only field names
are not included.'
| def get_all_field_names(self):
| try:
cache = self._name_map
except AttributeError:
cache = self.init_name_map()
names = sorted(cache.keys())
return [val for val in names if (not val.endswith(u'+'))]
|
'Initialises the field name -> field object mapping.'
| def init_name_map(self):
| cache = {}
for (f, model) in self.get_all_related_m2m_objects_with_model():
cache[f.field.related_query_name()] = (f, model, False, True)
for (f, model) in self.get_all_related_objects_with_model():
cache[f.field.related_query_name()] = (f, model, False, False)
for (f, model) in self.get... |
'Returns a list of (related-object, model) pairs. Similar to
get_fields_with_model().'
| def get_all_related_objects_with_model(self, local_only=False, include_hidden=False, include_proxy_eq=False):
| try:
self._related_objects_cache
except AttributeError:
self._fill_related_objects_cache()
predicates = []
if local_only:
predicates.append((lambda k, v: (not v)))
if (not include_hidden):
predicates.append((lambda k, v: (not k.field.rel.is_hidden())))
cache = (se... |
'Returns a list of (related-m2m-object, model) pairs. Similar to
get_fields_with_model().'
| def get_all_related_m2m_objects_with_model(self):
| try:
cache = self._related_many_to_many_cache
except AttributeError:
cache = self._fill_related_many_to_many_cache()
return list(six.iteritems(cache))
|
'Returns a list of parent classes leading to \'model\' (order from closet
to most distant ancestor). This has to handle the case were \'model\' is
a granparent or even more distant relation.'
| def get_base_chain(self, model):
| if (not self.parents):
return
if (model in self.parents):
return [model]
for parent in self.parents:
res = parent._meta.get_base_chain(model)
if res:
res.insert(0, parent)
return res
raise TypeError((u'%r is not an ancestor of thi... |
'Returns a list of all the ancestor of this model as a list. Useful for
determining if something is an ancestor, regardless of lineage.'
| def get_parent_list(self):
| result = set()
for parent in self.parents:
result.add(parent)
result.update(parent._meta.get_parent_list())
return result
|
'Returns the field on the current model which points to the given
"ancestor". This is possible an indirect link (a pointer to a parent
model, which points, eventually, to the ancestor). Used when
constructing table joins for model inheritance.
Returns None if the model isn\'t an ancestor of this one.'
| def get_ancestor_link(self, ancestor):
| if (ancestor in self.parents):
return self.parents[ancestor]
for parent in self.parents:
parent_link = parent._meta.get_ancestor_link(ancestor)
if parent_link:
return (self.parents[parent] or parent_link)
|
'Returns a list of Options objects that are ordered with respect to this object.'
| def get_ordered_objects(self):
| if (not hasattr(self, u'_ordered_objects')):
objects = []
self._ordered_objects = objects
return self._ordered_objects
|
'Returns choices with a default blank choices included, for use
as SelectField choices for this field.
Analogue of django.db.models.fields.Field.get_choices, provided
initially for utilisation by RelatedFieldListFilter.'
| def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_to_currently_related=False):
| first_choice = ((include_blank and blank_choice) or [])
queryset = self.model._default_manager.all()
if limit_to_currently_related:
queryset = queryset.complex_filter({('%s__isnull' % self.parent_model._meta.module_name): False})
lst = [(x._get_pk_val(), smart_text(x)) for x in queryset]
ret... |
'Get the fields in this class that should be edited inline.'
| def editable_fields(self):
| return [f for f in (self.opts.fields + self.opts.many_to_many) if (f.editable and (f != self.field))]
|
'Fill in all the cache information. This method is threadsafe, in the
sense that every caller will see the same state upon return, and if the
cache is already initialised, it does no work.'
| def _populate(self):
| if self.loaded:
return
imp.acquire_lock()
try:
if self.loaded:
return
for app_name in settings.INSTALLED_APPS:
if (app_name in self.handled):
continue
self.load_app(app_name, True)
if (not self.nesting_level):
fo... |
'Return app_label for given models module.'
| def _label_for(self, app_mod):
| return app_mod.__name__.split('.')[(-2)]
|
'Loads the app with the provided fully qualified name, and returns the
model module.'
| def load_app(self, app_name, can_postpone=False):
| self.handled[app_name] = None
self.nesting_level += 1
app_module = import_module(app_name)
try:
models = import_module('.models', app_name)
except ImportError:
self.nesting_level -= 1
if (not module_has_submodule(app_module, 'models')):
return None
elif ca... |
'Returns true if the model cache is fully populated.
Useful for code that wants to cache the results of get_models() for
themselves once it is safe to do so.'
| def app_cache_ready(self):
| return self.loaded
|
'Returns a list of all installed modules that contain models.'
| def get_apps(self):
| self._populate()
apps = [(v, k) for (k, v) in self.app_store.items()]
apps.sort()
return [elt[1] for elt in apps]
|
'Returns the module containing the models for the given app_label. If
the app has no models in it and \'emptyOK\' is True, returns None.'
| def get_app(self, app_label, emptyOK=False):
| self._populate()
imp.acquire_lock()
try:
for app_name in settings.INSTALLED_APPS:
if (app_label == app_name.split('.')[(-1)]):
mod = self.load_app(app_name, False)
if (mod is None):
if emptyOK:
return None
... |
'Returns the map of known problems with the INSTALLED_APPS.'
| def get_app_errors(self):
| self._populate()
return self.app_errors
|
'Given a module containing models, returns a list of the models.
Otherwise returns a list of all installed models.
By default, auto-created models (i.e., m2m models without an
explicit intermediate table) are not included. However, if you
specify include_auto_created=True, they will be.
By default, models created to sa... | def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False, only_installed=True, include_swapped=False):
| cache_key = (app_mod, include_auto_created, include_deferred, only_installed, include_swapped)
try:
return self._get_models_cache[cache_key]
except KeyError:
pass
self._populate()
if app_mod:
if (app_mod in self.app_store):
app_list = [self.app_models.get(self._la... |
'Returns the model matching the given app_label and case-insensitive
model_name.
Returns None if no model is found.'
| def get_model(self, app_label, model_name, seed_cache=True, only_installed=True):
| if seed_cache:
self._populate()
if (only_installed and (app_label not in self.app_labels)):
return None
return self.app_models.get(app_label, SortedDict()).get(model_name.lower())
|
'Register a set of models as belonging to an app.'
| def register_models(self, app_label, *models):
| for model in models:
model_name = model._meta.object_name.lower()
model_dict = self.app_models.setdefault(app_label, SortedDict())
if (model_name in model_dict):
fname1 = os.path.abspath(upath(sys.modules[model.__module__].__file__))
fname2 = os.path.abspath(upath(sys... |
'Add a node to the where-tree. If the data is a list or tuple, it is
expected to be of the form (obj, lookup_type, value), where obj is
a Constraint object, and is then slightly munged before being stored
(to avoid storing any reference to field objects). Otherwise, the \'data\'
is stored unchanged and can be any class... | def add(self, data, connector):
| if (not isinstance(data, (list, tuple))):
super(WhereNode, self).add(data, connector)
return
(obj, lookup_type, value) = data
if isinstance(value, collections.Iterator):
value = list(value)
if isinstance(value, datetime.datetime):
value_annotation = datetime.datetime
... |
'Returns the SQL version of the where clause and the value to be
substituted in. Returns \'\', [] if this node matches everything,
None, [] if this node is empty, and raises EmptyResultSet if this
node can\'t match anything.'
| def as_sql(self, qn, connection):
| result = []
result_params = []
(everything_childs, nothing_childs) = (0, 0)
non_empty_childs = len(self.children)
for child in self.children:
try:
if hasattr(child, 'as_sql'):
(sql, params) = child.as_sql(qn=qn, connection=connection)
else:
... |
'Turn a tuple (Constraint(table_alias, column_name, db_type),
lookup_type, value_annotation, params) into valid SQL.
The first item of the tuple may also be an Aggregate.
Returns the string for the SQL fragment and the parameters to use for
it.'
| def make_atom(self, child, qn, connection):
| (lvalue, lookup_type, value_annotation, params_or_value) = child
if isinstance(lvalue, Constraint):
try:
(lvalue, params) = lvalue.process(lookup_type, params_or_value, connection)
except EmptyShortCircuit:
raise EmptyResultSet
elif isinstance(lvalue, Aggregate):
... |
'Returns the SQL fragment used for the left-hand side of a column
constraint (for example, the "T1.foo" portion in the clause
"WHERE ... T1.foo = 6").'
| def sql_for_columns(self, data, qn, connection):
| (table_alias, name, db_type) = data
if table_alias:
lhs = ('%s.%s' % (qn(table_alias), qn(name)))
else:
lhs = qn(name)
return (connection.ops.field_cast_sql(db_type) % lhs)
|
'Relabels the alias values of any children. \'change_map\' is a dictionary
mapping old (current) alias values to the new values.'
| def relabel_aliases(self, change_map, node=None):
| if (not node):
node = self
for (pos, child) in enumerate(node.children):
if hasattr(child, 'relabel_aliases'):
child.relabel_aliases(change_map)
elif isinstance(child, tree.Node):
self.relabel_aliases(change_map, child)
elif isinstance(child, (list, tuple)... |
'Save the state of the Constraint for pickling.
Fields aren\'t necessarily pickleable, because they can have
callable default values. So, instead of pickling the field
store a reference so we can restore it manually'
| def __getstate__(self):
| obj_dict = self.__dict__.copy()
if self.field:
obj_dict['model'] = self.field.model
obj_dict['field_name'] = self.field.name
del obj_dict['field']
return obj_dict
|
'Restore the constraint'
| def __setstate__(self, data):
| model = data.pop('model', None)
field_name = data.pop('field_name', None)
self.__dict__.update(data)
if (model is not None):
self.field = model._meta.get_field(field_name)
else:
self.field = None
|
'Returns a tuple of data suitable for inclusion in a WhereNode
instance.'
| def process(self, lookup_type, value, connection):
| from django.db.models.base import ObjectDoesNotExist
try:
if self.field:
params = self.field.get_db_prep_lookup(lookup_type, value, connection=connection, prepared=True)
db_type = self.field.db_type(connection=connection)
else:
params = Field().get_db_prep_loo... |
'Convert the database-returned value into a type that is consistent
across database backends.
By default, this defers to the underlying backend operations, but
it can be overridden by Query classes for specific backends.'
| def convert_values(self, value, field, connection):
| return connection.ops.convert_values(value, field)
|
'Returns the query as a string of SQL with the parameter values
substituted in (use sql_with_params() to see the unsubstituted string).
Parameter values won\'t necessarily be quoted correctly, since that is
done by the database interface at execution time.'
| def __str__(self):
| (sql, params) = self.sql_with_params()
return (sql % params)
|
'Returns the query as an SQL string and the parameters that will be
subsituted into the query.'
| def sql_with_params(self):
| return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
|
'Pickling support.'
| def __getstate__(self):
| obj_dict = self.__dict__.copy()
obj_dict['related_select_fields'] = []
obj_dict['related_select_cols'] = []
obj_dict['select_fields'] = [(((f is not None) and f.name) or None) for f in obj_dict['select_fields']]
return obj_dict
|
'Unpickling support.'
| def __setstate__(self, obj_dict):
| opts = obj_dict['model']._meta
obj_dict['select_fields'] = [(((name is not None) and opts.get_field(name)) or None) for name in obj_dict['select_fields']]
self.__dict__.update(obj_dict)
|
'Returns the Options instance (the model._meta) from which to start
processing. Normally, this is self.model._meta, but it can be changed
by subclasses.'
| def get_meta(self):
| return self.model._meta
|
'Creates a copy of the current instance. The \'kwargs\' parameter can be
used by clients to update attributes after copying has taken place.'
| def clone(self, klass=None, memo=None, **kwargs):
| obj = Empty()
obj.__class__ = (klass or self.__class__)
obj.model = self.model
obj.alias_refcount = self.alias_refcount.copy()
obj.alias_map = self.alias_map.copy()
obj.table_map = self.table_map.copy()
obj.join_map = self.join_map.copy()
obj.default_cols = self.default_cols
obj.defa... |
'Convert the database-returned value into a type that is consistent
across database backends.
By default, this defers to the underlying backend operations, but
it can be overridden by Query classes for specific backends.'
| def convert_values(self, value, field, connection):
| return connection.ops.convert_values(value, field)
|
'Resolve the value of aggregates returned by the database to
consistent (and reasonable) types.
This is required because of the predisposition of certain backends
to return Decimal and long types when they are not needed.'
| def resolve_aggregate(self, value, aggregate, connection):
| if (value is None):
if aggregate.is_ordinal:
return 0
return value
elif aggregate.is_ordinal:
return int(value)
elif aggregate.is_computed:
return float(value)
else:
return self.convert_values(value, aggregate.field, connection)
|
'Returns the dictionary with the values of the existing aggregations.'
| def get_aggregation(self, using):
| if (not self.aggregate_select):
return {}
if (self.group_by is not None):
from django.db.models.sql.subqueries import AggregateQuery
query = AggregateQuery(self.model)
obj = self.clone()
for (alias, aggregate) in self.aggregate_select.items():
if aggregate.is_... |
'Performs a COUNT() query using the current filter constraints.'
| def get_count(self, using):
| obj = self.clone()
if ((len(self.select) > 1) or self.aggregate_select or (self.distinct and self.distinct_fields)):
from django.db.models.sql.subqueries import AggregateQuery
subquery = obj
subquery.clear_ordering(True)
subquery.clear_limits()
obj = AggregateQuery(obj.mo... |
'Merge the \'rhs\' query into the current one (with any \'rhs\' effects
being applied *after* (that is, "to the right of") anything in the
current query. \'rhs\' is not modified during a call to this function.
The \'connector\' parameter describes how to connect filters from the
\'rhs\' query.'
| def combine(self, rhs, connector):
| assert (self.model == rhs.model), 'Cannot combine queries on two different base models.'
assert self.can_filter(), 'Cannot combine queries once a slice has been taken.'
assert (self.distinct == rhs.distinct), 'Cannot combine a unique query with ... |
'Converts the self.deferred_loading data structure to an alternate data
structure, describing the field that *will* be loaded. This is used to
compute the columns to select from the database and also by the
QuerySet class to work out which fields are being initialised on each
model. Models that have all their fields in... | def deferred_to_data(self, target, callback):
| (field_names, defer) = self.deferred_loading
if (not field_names):
return
orig_opts = self.model._meta
seen = {}
must_include = {orig_opts.concrete_model: set([orig_opts.pk])}
for field_name in field_names:
parts = field_name.split(LOOKUP_SEP)
cur_model = self.model
... |
'Callback used by deferred_to_columns(). The "target" parameter should
be a set instance.'
| def deferred_to_columns_cb(self, target, model, fields):
| table = model._meta.db_table
if (table not in target):
target[table] = set()
for field in fields:
target[table].add(field.column)
|
'Returns a table alias for the given table_name and whether this is a
new alias or not.
If \'create\' is true, a new alias is always created. Otherwise, the
most recently created alias for the table (if one exists) is reused.'
| def table_alias(self, table_name, create=False):
| current = self.table_map.get(table_name)
if ((not create) and current):
alias = current[0]
self.alias_refcount[alias] += 1
return (alias, False)
if current:
alias = ('%s%d' % (self.alias_prefix, (len(self.alias_map) + 1)))
current.append(alias)
else:
alias... |
'Increases the reference count for this alias.'
| def ref_alias(self, alias):
| self.alias_refcount[alias] += 1
|
'Decreases the reference count for this alias.'
| def unref_alias(self, alias, amount=1):
| self.alias_refcount[alias] -= amount
|
'Promotes recursively the join type of given aliases and its children to
an outer join. If \'unconditional\' is False, the join is only promoted if
it is nullable or the parent join is an outer join.
Note about join promotion: When promoting any alias, we make sure all
joins which start from that alias are promoted, to... | def promote_joins(self, aliases, unconditional=False):
| aliases = list(aliases)
while aliases:
alias = aliases.pop(0)
if (self.alias_map[alias].rhs_join_col is None):
continue
parent_alias = self.alias_map[alias].lhs_alias
parent_louter = (parent_alias and (self.alias_map[parent_alias].join_type == self.LOUTER))
al... |
'This method will reset reference counts for aliases so that they match
the value passed in :param to_counts:.'
| def reset_refcounts(self, to_counts):
| for (alias, cur_refcount) in self.alias_refcount.copy().items():
unref_amount = (cur_refcount - to_counts.get(alias, 0))
self.unref_alias(alias, unref_amount)
|
'Given a "before" copy of the alias_refcounts dictionary (as
\'initial_refcounts\') and a collection of aliases that may have been
changed or created, works out which aliases have been created since
then and which ones haven\'t been used and promotes all of those
aliases, plus any children of theirs in the alias tree, ... | def promote_unused_aliases(self, initial_refcounts, used_aliases):
| for alias in self.tables:
if ((alias in used_aliases) and ((alias not in initial_refcounts) or (self.alias_refcount[alias] == initial_refcounts[alias]))):
self.promote_joins([alias])
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.