def get_deleted(self, model_class, db=None, model_db=None):
"""
Returns all the deleted versions for the given model class.
The results are returned with the most recent versions first.
"""
db = db or DEFAULT_DB_ALIAS
model_db = model_db or db
content_type = ContentType.objects.db_manager(db).get_for_model(model_class)
live_pk_queryset = model_class._default_manager.db_manager(model_db).all().values_list("pk", flat=True)
versioned_objs = self._get_versions(db).filter(
content_type = content_type,
)
if has_int_pk(model_class):
# If the model and version data are in different databases, decouple the queries.
if model_db != db:
live_pk_queryset = list(live_pk_queryset.iterator())
# We can do this as a fast, in-database join.
deleted_version_pks = versioned_objs.exclude(
object_id_int__in = live_pk_queryset
).values_list("object_id_int")
else:
# This join has to be done as two separate queries.
deleted_version_pks = versioned_objs.exclude(
object_id__in = list(live_pk_queryset.iterator())
).values_list("object_id")
deleted_version_pks = deleted_version_pks.exclude(
type = VERSION_DELETE,
).annotate(
latest_pk = Max("pk")
).values_list("latest_pk", flat=True)
# HACK: MySQL deals extremely badly with this as a subquery, and can hang infinitely.
# TODO: If a version is identified where this bug no longer applies, we can add a version specifier.
if connection.vendor == "mysql":
deleted_version_pks = list(deleted_version_pks)
# Return the deleted versions!
return self._get_versions(db).filter(pk__in=deleted_version_pks).order_by("-pk")
# Signal receivers.
评论列表
文章目录