本文整理汇总了Python中django.db.models.deletion.Collector方法的典型用法代码示例。如果您正苦于以下问题:Python deletion.Collector方法的具体用法?Python deletion.Collector怎么用?Python deletion.Collector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.deletion
的用法示例。
在下文中一共展示了deletion.Collector方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self):
"""
Deletes the records in the current QuerySet.
"""
assert self.query.can_filter(), \
"Cannot use 'limit' or 'offset' with delete."
del_query = self._clone()
# The delete is actually 2 queries - one to find related objects,
# and one to delete. Make sure that the discovery of related
# objects is performed on the same database as the deletion.
del_query._for_write = True
# Disable non-supported fields.
del_query.query.select_for_update = False
del_query.query.select_related = False
del_query.query.clear_ordering(force_empty=True)
collector = Collector(using=del_query.db)
collector.collect(del_query)
collector.delete()
# Clear the result cache, in case this QuerySet gets reused.
self._result_cache = None
示例2: setup_simple_attributes
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def setup_simple_attributes(cls):
for (attrname, default) in cls.simple_attributes:
setattr(
cls,
attrname,
property(
lambda self, attrname=attrname, default=default: self.get_attribute(
attrname, default
),
lambda self, value, attrname=attrname: self.set_attribute(
attrname, value
),
None,
attrname,
),
)
# For cascading deletes, we need a Collector that doesn't do fastdelete,
# so we get a full list of models.
示例3: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self):
"""
Deletes the records in the current QuerySet.
"""
assert self.query.can_filter(), \
"Cannot use 'limit' or 'offset' with delete."
del_query = self._clone()
# The delete is actually 2 queries - one to find related objects,
# and one to delete. Make sure that the discovery of related
# objects is performed on the same database as the deletion.
del_query._for_write = True
# Disable non-supported fields.
del_query.query.select_for_update = False
del_query.query.select_related = False
del_query.query.clear_ordering(force_empty=True)
collector = Collector(using=del_query.db)
collector.collect(del_query)
collector.delete()
# Clear the result cache, in case this QuerySet gets reused.
self._result_cache = None
示例4: delete_without_cascade
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete_without_cascade(self, using=None, keep_parents=False):
"""
Modified version of django's default delete() method.
This method is added to enable safe deletion of the child models without
removing objects related to it through the parent. As of Feb 2017,
no models are directly related to the OAuth2DataRequestProject or
OnSiteDataRequestProject child models.
"""
allowed_models = [
"private_sharing.onsitedatarequestproject",
"private_sharing.oauth2datarequestproject",
]
if self._meta.label_lower not in allowed_models:
raise Exception("'delete_without_cascade' only for child models!")
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, (
"%s object can't be deleted because its %s attribute is set to None."
% (self._meta.object_name, self._meta.pk.attname)
)
collector = Collector(using=using)
collector.collect([self], keep_parents=keep_parents, collect_related=False)
return collector.delete()
示例5: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self, using=None):
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, (
"%s object can't be deleted because its %s attribute is set to None." %
(self._meta.object_name, self._meta.pk.attname)
)
collector = Collector(using=using)
collector.collect([self])
collector.delete()
示例6: as_sql
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def as_sql(self):
""" Generate SQL queries that perform related deletion """
# List of (sql, params) tuples to perform deletion
query_list = []
for model, instances in self.data.items():
self.data[model] = sorted(instances, key=attrgetter("pk"))
self.sort()
# Do not send pre_delete signals as in .delete()
# Fast deletes
for qs in self.fast_deletes:
# TODO Check for any potential caveats from complex queries - assume none are generated by Collector
# Clone queryset into DeleteQuery to use .as_sql()
query_list.append(qs.query.clone(klass=sql.DeleteQuery).get_compiler(self.using).as_sql())
# update fields
for model, instances_for_fieldvalues in six.iteritems(self.field_updates):
query = sql.UpdateQuery(model)
for (field, value), instances in six.iteritems(instances_for_fieldvalues):
query.add_update_values({field.name: value})
query.add_q(models.Q(pk__in=[obj.pk for obj in instances]))
query_list.append(query.get_compiler(using=self.using).as_sql())
# reverse instance collections
for instances in six.itervalues(self.data):
instances.reverse()
# delete instances
for model, instances in six.iteritems(self.data):
query = sql.DeleteQuery(model)
pk_list = [obj.pk for obj in instances]
query.where = query.where_class()
query.add_q(models.Q(pk__in=pk_list))
query_list.append(query.get_compiler(using=self.using).as_sql())
# Do not update instances as in .delete()
return query_list
示例7: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self, using=None, keep_parents=False):
using = using or router.db_for_write(self.__class__, instance=self)
assert self.pk is not None, (
"%s object can't be deleted because its %s attribute is set to None." %
(self._meta.object_name, self._meta.pk.attname)
)
collector = Collector(using=using)
collector.collect([self], keep_parents=keep_parents)
return collector.delete()
示例8: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self):
"""Delete the records in the current QuerySet."""
assert self.query.can_filter(), \
"Cannot use 'limit' or 'offset' with delete."
if self._fields is not None:
raise TypeError("Cannot call delete() after .values() or .values_list()")
del_query = self._chain()
# The delete is actually 2 queries - one to find related objects,
# and one to delete. Make sure that the discovery of related
# objects is performed on the same database as the deletion.
del_query._for_write = True
# Disable non-supported fields.
del_query.query.select_for_update = False
del_query.query.select_related = False
del_query.query.clear_ordering(force_empty=True)
collector = Collector(using=del_query.db)
collector.collect(del_query)
deleted, _rows_count = collector.delete()
# Clear the result cache, in case this QuerySet gets reused.
self._result_cache = None
return deleted, _rows_count
示例9: _collect_related
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def _collect_related(self, using=None, keep_parents=False):
collector = Collector(using=using)
collector.collect([self], keep_parents=keep_parents)
fast_deletes = []
for queryset in collector.fast_deletes:
if queryset.count() > 0:
fast_deletes.append(queryset)
return dict(
instances_with_model=collector.instances_with_model(), fast_deletes=fast_deletes, data=collector.data
)
示例10: get_cascades
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def get_cascades(self, user):
c = Collector(user._state.db)
c.collect([user])
# figure out which models this user has data in
existing = set(c.data.keys())
existing.update(q.model for q in c.fast_deletes if q.exists())
# but don't mention they have a User, that's obvious:
existing.discard(User)
return existing
示例11: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self, using=None, keep_parents=False):
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, (
"%s object can't be deleted because its %s attribute is set to None." %
(self._meta.object_name, self._meta.pk.attname)
)
collector = Collector(using=using)
collector.collect([self], keep_parents=keep_parents)
return collector.delete()
示例12: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self):
"""
Deletes the records in the current QuerySet.
"""
assert self.query.can_filter(), \
"Cannot use 'limit' or 'offset' with delete."
if self._fields is not None:
raise TypeError("Cannot call delete() after .values() or .values_list()")
del_query = self._clone()
# The delete is actually 2 queries - one to find related objects,
# and one to delete. Make sure that the discovery of related
# objects is performed on the same database as the deletion.
del_query._for_write = True
# Disable non-supported fields.
del_query.query.select_for_update = False
del_query.query.select_related = False
del_query.query.clear_ordering(force_empty=True)
collector = Collector(using=del_query.db)
collector.collect(del_query)
deleted, _rows_count = collector.delete()
# Clear the result cache, in case this QuerySet gets reused.
self._result_cache = None
return deleted, _rows_count
示例13: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self, *args, **kwargs):
"""
Replace super(BaseModel, self).delete()
Cause: When delete relationship in cascade default no have attribute User to Log.
"""
using = router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (
self._meta.object_name, self._meta.pk.attname)
collector = Collector(using=using)
collector.collect([self])
collector.delete()
示例14: delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def delete(self, using=None):
using = using or router.db_for_write(self.__class__, instance=self)
assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
collector = Collector(using=using)
collector.collect([self])
collector.delete()
示例15: modal_delete
# 需要导入模块: from django.db.models import deletion [as 别名]
# 或者: from django.db.models.deletion import Collector [as 别名]
def modal_delete(request, s, *args, **kwargs):
server = Server.objects.get(id=s)
collector = Collector(using='default')
collector.collect([server])
estimate = sum(len(x) for x in collector.data.values())
breakdown = {}
for k, v in collector.data.items():
name = k._meta.verbose_name_plural if len(v) != 1 else k._meta.verbose_name
breakdown[name] = len(v)
return render(request, 'components/servers/detailed/modals/delete.pug',
{'estimate': estimate,
'breakdown': breakdown})