Hello,
I have been trying to figure out how to customize the storage location of my media that is placed in my ckeditor rich text field.
I have a function that I use when adding images def get_upload_path(instance, filename): model = instance.album.model.__class__._meta name = model.verbose_name_plural.replace(' ', '_') album = instance.album.name return f'blog/images/{name}/{album}/images/{filename}'
django_ckeditor_5 defaults the images into the media directory. Is there a way to make it do something similar to what I am doing in my get_upload_path function?
In the documentation it gives this example: class CustomStorage(FileSystemStorage): """Custom storage for django_ckeditor_5 images.""" location = os.path.join(settings.MEDIA_ROOT, "django_ckeditor_5") base_url = urljoin(settings.MEDIA_URL, "django_ckeditor_5/")
I have tried using this in my settings file and I still get the same results.
Wow you really helped me with your CustomStorage idea, thanks! Here is how I made the year/month/day structure for my own ckeditor5:
from django.utils.timezone import now
class CustomStorage(FileSystemStorage):
location = os.path.join(settings.MEDIA_ROOT, "uploads", str(now().year), f"{now().month:02d}", f"{now().day:02d}")
base_url = urljoin(settings.MEDIA_URL, f"uploads/{now().year}/{now().month:02d}/{now().day:02d}/")```
Hope it helps!