Ticket #5374: modelamin.patch

File modelamin.patch, 5.4 KB (added by danielr, 8 years ago)

Initial attempt at patch.

  • django/core/management/validation.py

     
    11import sys
    22from django.core.management.color import color_style
     3from django.contrib import admin
    34
    45class ModelErrorCollection:
    56    def __init__(self, outfile=sys.stdout):
     
    132133                        e.add(opts, "Reverse query name for m2m field '%s' clashes with related field '%s.%s'. Add a related_name argument to the definition for '%s'." % (f.name, rel_opts.object_name, r.get_accessor_name(), f.name))
    133134
    134135        # Check admin attribute.
    135         if opts.admin is not None:
     136        if cls in admin.site._registry:
     137            admin_opts = admin.site._registry[cls]
    136138            # prepopulated_fields
    137             if not isinstance(opts.admin.prepopulated_fields, dict):
    138                 e.add(opts, '"%s": prepopulated_fields should be a dictionary.' % f.name)
     139            if not isinstance(admin_opts.prepopulated_fields, dict):
     140                e.add(opts, 'admin: prepopulated_fields should be a dictionary.')
    139141            else:
    140                 for field_name, field_list in opts.admin.prepopulated_fields.items():
     142                for field_name, field_list in admin_opts.prepopulated_fields.items():
    141143                    if not isinstance(field_list, (list, tuple)):
    142                         e.add(opts, '"%s": prepopulated_fields "%s" value should be a list or tuple.' % (f.name, field_name))
     144                        e.add(opts, 'admin: prepopulated_fields "%s" value should be a list or tuple.' % (field_name))
    143145
    144146            # list_display
    145             if not isinstance(opts.admin.list_display, (list, tuple)):
     147            if not isinstance(admin_opts.list_display, (list, tuple)):
    146148                e.add(opts, '"admin.list_display", if given, must be set to a list or tuple.')
    147149            else:
    148                 for fn in opts.admin.list_display:
     150                for fn in admin_opts.list_display:
    149151                    try:
    150152                        f = opts.get_field(fn)
    151153                    except models.FieldDoesNotExist:
     
    155157                        if isinstance(f, models.ManyToManyField):
    156158                            e.add(opts, '"admin.list_display" doesn\'t support ManyToManyFields (%r).' % fn)
    157159            # list_display_links
    158             if opts.admin.list_display_links and not opts.admin.list_display:
     160            if admin_opts.list_display_links and not admin_opts.list_display:
    159161                e.add(opts, '"admin.list_display" must be defined for "admin.list_display_links" to be used.')
    160             if not isinstance(opts.admin.list_display_links, (list, tuple)):
     162            if not isinstance(admin_opts.list_display_links, (list, tuple)):
    161163                e.add(opts, '"admin.list_display_links", if given, must be set to a list or tuple.')
    162164            else:
    163                 for fn in opts.admin.list_display_links:
     165                for fn in admin_opts.list_display_links:
    164166                    try:
    165167                        f = opts.get_field(fn)
    166168                    except models.FieldDoesNotExist:
    167169                        if not hasattr(cls, fn):
    168170                            e.add(opts, '"admin.list_display_links" refers to %r, which isn\'t an attribute, method or property.' % fn)
    169                     if fn not in opts.admin.list_display:
     171                    if fn not in admin_opts.list_display:
    170172                        e.add(opts, '"admin.list_display_links" refers to %r, which is not defined in "admin.list_display".' % fn)
    171173            # list_filter
    172             if not isinstance(opts.admin.list_filter, (list, tuple)):
     174            if not isinstance(admin_opts.list_filter, (list, tuple)):
    173175                e.add(opts, '"admin.list_filter", if given, must be set to a list or tuple.')
    174176            else:
    175                 for fn in opts.admin.list_filter:
     177                for fn in admin_opts.list_filter:
    176178                    try:
    177179                        f = opts.get_field(fn)
    178180                    except models.FieldDoesNotExist:
    179181                        e.add(opts, '"admin.list_filter" refers to %r, which isn\'t a field.' % fn)
    180182            # date_hierarchy
    181             if opts.admin.date_hierarchy:
     183            if admin_opts.date_hierarchy:
    182184                try:
    183                     f = opts.get_field(opts.admin.date_hierarchy)
     185                    f = opts.get_field(admin_opts.date_hierarchy)
    184186                except models.FieldDoesNotExist:
    185                     e.add(opts, '"admin.date_hierarchy" refers to %r, which isn\'t a field.' % opts.admin.date_hierarchy)
     187                    e.add(opts, '"admin.date_hierarchy" refers to %r, which isn\'t a field.' % admin_opts.date_hierarchy)
     188            # raw_id_admin
     189            if not isinstance(admin_opts.raw_id_fields, (list, tuple)):
     190                e.add(opts, '"admin.raw_id_fields", if given, must be set to a list or tuple.')
     191            else:
     192                for fn in admin_opts.raw_id_fields:
     193                    try:
     194                        f = opts.get_field(fn)
     195                    except models.FieldDoesNotExist:
     196                        e.add(opts, '"admin.raw_id_fields" refers to %r, which isn\'t a field.' % fn)
    186197
     198
    187199        # Check ordering attribute.
    188200        if opts.ordering:
    189201            for field_name in opts.ordering:
Back to Top