querysets.py 947 B

1234567891011121314151617181920212223242526
  1. from django.apps import apps
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.db.utils import ProgrammingError
  4. from utilities.querysets import RestrictedQuerySet
  5. __all__ = (
  6. 'ObjectChangeQuerySet',
  7. )
  8. class ObjectChangeQuerySet(RestrictedQuerySet):
  9. def valid_models(self):
  10. # Exclude any change records which refer to an instance of a model that's no longer installed. This
  11. # can happen when a plugin is removed but its data remains in the database, for example.
  12. try:
  13. content_types = ContentType.objects.get_for_models(*apps.get_models()).values()
  14. except ProgrammingError:
  15. # Handle the case where the database schema has not yet been initialized
  16. content_types = ContentType.objects.none()
  17. content_type_ids = set(
  18. ct.pk for ct in content_types
  19. )
  20. return self.filter(changed_object_type_id__in=content_type_ids)