Tuesday, September 8, 2015

Adding dynamic methods to ModelAdmin fails in 1.8

Hi,

I'm trying to add a dynamic method to a ModelAdmin so that I can have automatically generated thumbnail fields for any image by simply adding '<fieldname>_thumbnail' to the field definitions - which saves a lot of code when there's a load of admin classes with image fields that need thumbnails.

e.g.

class ThumbnailMixin(object):
    def getattr(self, name):
         if name.endswith('_thumbnail'):
              # ... generate appropriate thumbnail method and return
              return thumbnail_function
         else:
             raise AttributeError

class ExampleAdmin(models.ModelAdmin, ThumbnailMixin):
     fields = ['image_thumbnail', 'image', ...]

This worked fine in Django 1.6/1.7, but it's now failing in 1.8 with a SystemCheckError (admin.E035 / admin.E108). If I silence the error, the admin actually works fine, but that feels icky.

The check fails because it checks for the field being on the ExampleAdmin class, not on an instance - whereas the admin always uses an instance, so it works.

What's the "right" way to do this in the 1.8 world - I've considered a custom metaclass, but that feels like overkill for a simple task like this. Should this even work, and if so is it a bug in the check framework?

Cheers,

Malcolm

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/987461ae-f455-44f7-9c61-935a6d412c49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment