Ticket #18682: django_fix_18682.patch

File django_fix_18682.patch, 1.8 KB (added by speijnik, 5 years ago)

Initial version of the patch

  • django/contrib/contenttypes/management.py

    diff --git a/django/contrib/contenttypes/management.py b/django/contrib/contenttypes/management.py
    index f705330..b2d6b55 100644
    a b def update_contenttypes(app_config, verbosity=2, interactive=True, using=DEFAULT 
    6161    # Confirm that the content type is stale before deletion.
    6262    if to_remove:
    6363        if interactive:
    64             content_type_display = '\n'.join(
    65                 '    %s | %s' % (ct.app_label, ct.model)
    66                 for ct in to_remove
    67             )
     64            ct_info = []
     65            for ct in to_remove:
     66                relations = ct._meta.get_all_related_objects()
     67                related_counts = {}
     68
     69                ct_info.append('    %s | %s' % (ct.app_label, ct.model))
     70
     71                for rel in relations:
     72                    # Ignore non-ContentType relations
     73                    if rel.parent_model != ContentType:
     74                        continue
     75
     76                    accessor = getattr(ct, rel.get_accessor_name(), None)
     77
     78                    if not accessor:
     79                        continue
     80
     81                    filter_kwargs = {
     82                        rel.field.name: ct
     83                    }
     84                    related_count =  accessor.filter(**filter_kwargs).count()
     85                    if related_count > 0:
     86                        ct_info.append('      - %s | %s object%s to be deleted: %d' % (
     87                            rel.opts.app_label,
     88                            rel.opts.model_name,
     89                            's' if related_count != 1 else '',
     90                            related_count)
     91                        )
     92
     93            content_type_display = '\n'.join(ct_info)
    6894            ok_to_delete = input("""The following content types are stale and need to be deleted:
    6995
    7096%s
Back to Top