Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a complete solution for ya using a form. I used admin views for this:</p> <pre><code>class MyInventoryItemForm(forms.ModelForm): class Meta: model = InventoryItem exclude = ['thumbnail', 'price', 'active'] def clean_photo(self): import StringIO image_field = self.cleaned_data['photo'] photo_new = StringIO.StringIO(image_field.read()) try: from PIL import Image, ImageOps except ImportError: import Image import ImageOps image = Image.open(photo_new) # ImageOps compatible mode if image.mode not in ("L", "RGB"): image = image.convert("RGB") image.thumbnail((200, 200), Image.ANTIALIAS) image_file = StringIO.StringIO() image.save(image_file, 'png') image_field.file = image_file return image_field </code></pre> <p>My inventory model looks like this:</p> <pre><code>class InventoryItem(models.Model): class Meta: ordering = ['name'] verbose_name_plural = "Items" def get_absolute_url(self): return "/products/{0}/".format(self.slug) def get_file_path(instance, filename): if InventoryItem.objects.filter(pk=instance.pk): cur_inventory = InventoryItem.objects.get(pk=instance.pk) if cur_inventory.photo: old_filename = str(cur_inventory.photo) os.remove(os.path.join(MEDIA_ROOT, old_filename)) ext = filename.split('.')[-1] filename = "{0}.{1}".format(uuid.uuid4(), ext) return os.path.join('inventory', filename) #return os.path.join(filename) def admin_image(self): return '&lt;img height="50px" src="{0}/{1}"/&gt;'.format(MEDIA_URL, self.photo) admin_image.allow_tags = True photo = models.ImageField(_('Image'), upload_to=get_file_path, storage=fs, blank=False, null=False) thumbnail = models.ImageField(_('Thumbnail'), upload_to="thumbnails/", storage=fs, blank=True, null=True) </code></pre> <p>....</p> <p>I ended overwriting the save function of the model instead to save the photo and a thumb instead of just resizing the photo:</p> <pre><code>def save(self): # Save this photo instance first super(InventoryItem, self).save() from PIL import Image from cStringIO import StringIO from django.core.files.uploadedfile import SimpleUploadedFile # Set our max thumbnail size in a tuple (max width, max height) THUMBNAIL_SIZE = (200, 200) # Open original photo which we want to thumbnail using PIL's Image object image = Image.open(os.path.join(MEDIA_ROOT, self.photo.name)) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) # Save the thumbnail temp_handle = StringIO() image.save(temp_handle, 'png') # image stored to stringIO temp_handle.seek(0) # sets position of file to 0 # Save to the thumbnail field suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1], temp_handle.read(), content_type='image/png') # reads in the file to save it self.thumbnail.save(suf.name+'.png', suf, save=False) #Save this photo instance again to save the thumbnail super(InventoryItem, self).save() </code></pre> <p>Both work great though depending on what you want to do :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload