sorl-thumbnail ImageWithThumbnailsField

Rambling on about Django 92 months ago.

If you're using sorl-thumbnail, you may want to update svn and check out the new field I recently added.

No docs as of this moment, so just look at the source.

What, you're not afraid of source are you?

Ok, if you insist, here's a summary:

To use, just replace your current ImageField with ImageWithThumbnailsField (which you import from sorl.thumbnail.fields).

It takes an extra argument: thumbnail, which is a dictionary containing the 2-length tuple of the size and optionally a list of thumbnail options. For example, if you used this field:

    photo = ImageWithThumbnailsField(
            'size': (50, 50),
            'options': ('crop',)

Your model instance will have two new functions:

Returns a DjangoThumbnail instance for your pleasure (if you use this in a template, it'll return the full URL to the thumbnail)
Returns the HTML <img src="..." width="..." height="..." alt="" /> tag. You may find this useful for showing the thumbnail in admin using list_display.

If you want to use multiple thumbnails, you can also provide a extra_thumbnails dictionary:

photo = ImageWithThumbnailsField(
        'size': (50, 50),
        'options': ('crop',)
        'admin': {
            'size': (60, 30)

Which would another two functions on your instance for each extra thumbnail. In this case, get_photo_admin_thumbnail and get_photo_admin_thumbnail_tag.

Leave a comment or see my other ramblings.

Comments (10)

Simon said: (91 months ago)

Thank you so much. This is perfect.

SAn said: (89 months ago)

realy cool, thanks a lot!

Daniel Jewett said: (86 months ago)

Hi Chris,
This is working well in admin list view (using get_image_thumbnail_tag(), but switching to add or change view produces an error with the following:

global name 'oldforms' is not defined

54. return [oldforms.ImageUploadField, oldforms.HiddenField] ...

▼ Local vars
Variable Value
<sorl.thumbnail.fields.ImageWithThumbnailsField object at 0x1977c50>

What can I do about that?

Thanks. Oh and beautiful baby!

Daniel Jewett said: (86 months ago)

Well, I added:

from django import oldforms

to 'sorl/thumbnails/'

Seems to work for now.

Dan J.

:) Chris said: (86 months ago)

Thanks for your feedback, Daniel. Stuff like this is best tracked in the issue tracker:

cocobuster said: (60 months ago)


I am having an issue with the thumbnail. Let say I have an image of 3000x4000, when the thumbnail is created the image orientations changes, my image was vertical, now it is horizontal.

any idea to force the resulting thumbnail to keep original orientation?

Prince said: (43 months ago)

Not able to download. No permission.

Prince said: (43 months ago)

not able to download. No Permission

Prince said: (43 months ago)

not able to download. No Permission

Prince said: (43 months ago)

not able to download. No Permission

Leave a comment