CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=dbe5573bb29c9b7b0a64554d; HttpOnly; Path=/; Secure
set-cookie: trac_session=223696b02e840eecf5166647; expires=Tue, 21 Oct 2025 05:44:46 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 05:44:46 GMT
x-served-by: cache-fra-etou8220085-FRA, cache-bom-vanm7210043-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753249486.391355,VS0,VE335
vary: Accept-Encoding
CustomImageField – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
from django.db.models import ImageField, FileField, signals from django.conf import settings import shutil, os, glob, re class CustomImageField(ImageField): """Allows model instance to specify upload_to dynamically. Model class should have a method like: def get_upload_to(self, attname): return 'path/to/%d' % self.id Based closely on: https://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/ Later updated for newforms-admin by jamstooks: https://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/ """ def __init__(self, *args, **kwargs): if 'upload_to' not in kwargs: kwargs['upload_to'] = 'tmp' self.use_key = kwargs.get('use_key', False) if 'use_key' in kwargs: del kwargs['use_key'] super(CustomImageField, self).__init__(*args, **kwargs) def contribute_to_class(self, cls, name): """Hook up events so we can access the instance.""" super(CustomImageField, self).contribute_to_class(cls, name) signals.post_save.connect(self._move_image, sender=cls) def _move_image(self, instance=None, **kwargs): """ Function to move the temporarily uploaded image to a more suitable directory using the model's get_upload_to() method. """ if hasattr(instance, 'get_upload_to'): src = getattr(instance, self.attname) if src: m = re.match(r"%s/(.*)" % self.upload_to, src) if m: if self.use_key: dst = os.path.join( instance.get_upload_to(self.attname), instance.id, m.group(1) ) else: dst = os.path.join( instance.get_upload_to(self.attname), m.group(1) ) basedir = os.path.join( settings.MEDIA_ROOT, os.path.dirname(dst) ) fromdir = os.path.join( settings.MEDIA_ROOT, src ) shutil.move(fromdir, basedir) setattr(instance, self.attname, dst) instance.save() def db_type(self): """Required by Django for ORM.""" return 'varchar(200)'
Last modified
15 years ago
Last modified on Mar 18, 2010, 11:40:52 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.