Don't forget to replace "self.image" by your image field name from your model ex ( self.cover )
replace Product by your model name
works pretty well :)
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully.
You need to have PILLOW installed for this to work.
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully.
This code runs well on Django 1.4 also with the Admin panel.
It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.
I use this snippet to save images to my imagefields on django models.
This uses the very awesome requests library, so install it first
pip install requests
You probably want to have this be done on a celery queu, if the image is big enough.
This widget allows you to display preview images with adjustable width and length of the link:
[example](http://img526.imageshack.us/img526/6588/screenshotat20111026215.png)
AdvancedFileInput(preview=True, image_width=200)
For other files, you can adjust the length of the link without preview:
[example](http://img845.imageshack.us/img845/6588/screenshotat20111026215.png)
AdvancedFileInput(preview=False, url_length=30)
by default, parameters are:
preview = True
url_length = 30
image_width = 200
Rather simple usage, modelforms/in the admin:
class CustomAdminForm(forms.ModelForm):
class Meta:
model = Something
widgets = {
'image': URLFileInput(default_exts=[".png", ".gif", ".jpg"]),
}
class SomethingAdmin(admin.ModelAdmin):
form = CustomAdminForm
admin.site.register(Something, SomethingAdmin)
Basically, this will pull the image from the URL instead of only pulling it from your harddrive for upload.
Also accepts optional default_exts argument which limits the file types. Defaults to images.
An update to snippet 1718 (http://www.djangosnippets.org/snippets/1718/). This update lets you pass an image URL string as well as a model ImageField instance. This means that you can then create thumbnails on demand for images outside of model
This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate.
Notes:
1. Validation of the field counts against your App Engine transformations quota.
2. This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well on App Engine; you should probably disable them.