Hi Malcom!

I would suggest you have a look at the ModelAdmin.get_fields() 
<https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_fields>
 
method to append your thumbnail read-only field names.

As documented 
<https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fields>,
 
just make sure you also override ModelAdmin.get_readonly_fields() 
<https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields>
 
to return them as well.

Cheers,
Simon

Le mardi 8 septembre 2015 13:31:47 UTC-4, Malcolm Box a écrit :
>
> 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+unsubscr...@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/05249551-899b-4a64-b416-216c9f81d950%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to