#25374: Admin system check prevents dynamic ModelAdmin attributes
-------------------------------+--------------------
     Reporter:  mbox           |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  contrib.admin  |    Version:  master
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 With a ModelAdmin that defines some dynamic attributes via __getattr__,
 and uses these in fields or readonly_fields (or list_display etc), the
 system check admin.E035 error is raised.

 Simple example below - note that in the full use case, the __getattr__ is
 part of a baseclass/mixin so it can be reused across multiple ModelAdmins
 :

 {{{
 class ExampleAdmin(models.ModelAdmin):
      fields = ['image_thumbnail', 'image', 'another_image',
 'another_image_thumbnail']
      readonly_fields = ['image_thumbnail', 'another_image_thumbnail']
      list_display = ['image_thumbnail']

      def __getattr__(self, name):
          if name.endswith('_thumbnail'):
               def thumbnail_function(obj):
                     # ... generate appropriate thumbnail method
               return thumbnail_function
          else:
              raise AttributeError
 }}}

 This code worked fine in 1.6, and it works fine in 1.8/1.9 if the system
 check is silenced.

 The root cause is that the system check uses the class to verify that
 fields exist rather than an instance:

 
https://github.com/django/django/blob/dae81c6ec62a76c1f28745ae3642c2d4a37ce259/django/contrib/admin/sites.py#L106-L110

 Which means that anything that's defined on an instance of the ModelAdmin
 but not on the class itself will fail the check, whereas the code will
 work.

 According to the documentation, for readonly_fields, the values are
 "similar to list_display", and list_display is documented to allow "A
 string representing an attribute on the ModelAdmin". However this fails
 system check admin.E108

 There's two issues raised by this:

 * Is it reasonable for system checks to fail when the actual code will
 work - or is this *always* a bug in the system check?
 * In this specific case, should the admin system check inspect ModelAdmin
 instances rather than classes so that the check is closer to the
 implementation.

--
Ticket URL: <https://code.djangoproject.com/ticket/25374>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.2c74f964cdcb22ece058af86029dd641%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to