This is a different take on polymorphic inheritance, inspired by SQLAlchemy's approach to the problem.
The common Django approach (e.g. snippets 1031 & 1034, [django_polymorphic](http://github.com/bconstantin/django_polymorphic)) is to use a foreign key to `ContentType` on the parent model and override `save()` to set the right content type automatically. That works fine but it might not always be possible or desirable, for example if there is another field that determines the "real type" of an instance.
In contrast this snippet (which is actually posted and maintained at [gist.github](http://gist.github.com/608595)) allows the user to explicitly specify the field that determines the real type of an instance. The basic idea and the terminology (`polymorphic_on` field, `polymorphic_identity` value) are taken from [SQLAlchemy](http://www.sqlalchemy.org/docs/orm/inheritance.html).
Some other features:
* It works for proxy child models too, with almost no overhead compared to non-polymorphic managers (since there's no need to hit another DB table as for multi-table inheritance).
* It does not override the default (or any other) model Manager. Regular (non-polymorphic) managers and querysets are still available if desired.
* It does not require extending a custom Model base class, using a custom metaclass, monkeypatching the models or any kind of magic.
ResizeImageField
================
(extension of RemovableImageField)
=================================
by Wim Feijen, Go2People.
What does it do?
----------------
ResizeImageField is a replacement for django's ImageField. It has two major benefits:
1. Creation of thumbnails and scaled images.
1. Extends the image upload form and adds a preview and a checkbox to remove the existing image.
It's easy to use:
- Replace ImageField by ResizeImageField
- No further changes are necessary
Requirements:
-------------
Working installation of PIL, the Python Imaging Library
Usage
-----
- add resize_image to your app
- add resize_filters.py to your templatetags
- in settings.py, set a PHOTO_DIR, f.e. photos/original
- in models.py, add:
- from settings import PHOTO_DIR
- from resize_image import ResizeImageField
- photo = ResizeImageField(upload_to=PHOTO_DIR, blank=True)
Scaled images will be stored in 'photos/scaled',
thumbnails will be stored in 'photos/thumb'.
Access your images from your template. Add::
{% load resize_filters %}
{{ address.photo.url|thumb }}
or::
{{ address.photo.url|scaled }}
Defaults
-------
- Scaled images are max. 200x200 pixels by default
- Thumbnails are 50x50 pixels.
Override the default behaviour in settings.py
Scaling is done by PIL's thumbnail function, transparency is conserved.
Credits
------
This code is an adaptation from python snippet 636 by tomZ: "Updated Filefield / ImageField with a delete checkbox"
- image
- thumbnail
- resize
- scale
- delete
- ImageField
- ResizeImageField
- removable