From ef3aace6b0d29c8b3b0fbba82e0000ac6d8d26a8 Mon Sep 17 00:00:00 2001 From: Daniel Kirkham Date: Mon, 17 Apr 2023 20:23:11 +1000 Subject: [PATCH] New show_references_index management command --- .../commands/show_references_index.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 wagtail/management/commands/show_references_index.py diff --git a/wagtail/management/commands/show_references_index.py b/wagtail/management/commands/show_references_index.py new file mode 100644 index 0000000000..478d476e03 --- /dev/null +++ b/wagtail/management/commands/show_references_index.py @@ -0,0 +1,36 @@ +from django.apps import apps +from django.contrib.contenttypes.models import ContentType +from django.core.management.base import BaseCommand + +from wagtail.models import ReferenceIndex + + +def model_name(model): + return f"{model.__module__}.{model.__name__}" + + +class Command(BaseCommand): + def handle(self, **options): + self.stdout.write("Reference index entries:") + object_count = 0 + + for model in sorted(apps.get_models(), key=lambda m: model_name(m)): + if not ReferenceIndex.is_indexed(model): + continue + + content_types = [ + ContentType.objects.get_for_model( + model_or_object, for_concrete_model=False + ) + for model_or_object in ([model] + model._meta.get_parent_list()) + ] + content_type = content_types[0] + base_content_type = content_types[-1] + + count = ReferenceIndex.objects.filter( + content_type=content_type, base_content_type=base_content_type + ).count() + self.stdout.write(f"{count:>6} {model_name(model)}") + object_count += count + + self.stdout.write(f"Total entries: {object_count}")