Search code examples
djangodjango-ckeditor

How to use django-ckeditor to specify different image upload paths for different models?


The usual practice is to define CKEDITOR_UPLOAD_PATH = 'uploads/' in settings.py. But this will save the images in all models in a unique path. Is it possible to assign a different image path to each model?

class BlogModel(models.Model):
    ......
    content = RichTextUploadingField() # image upload to /media/blog/

class NewsModel(models.Model):
    ......
    content = RichTextUploadingField() # image upload to /media/news/

Solution

  • You can assign a path by using FileField upload_to='pathname'

    class BlogModel(models.Model)
        content = models.FileField(upload_to='blog/')
    
    
    class NewsModel(models.Model)
        content = models.FileField(upload_to='news/')
    

    Blogmodel content will be uploaded to /media/blog/

    Newsmodel content will be uploaded to /media/news/

    You can also do:

    content = models.FileField(upload_to='pathname/%Y/%m/%d/')
    

    Will be saved as /pathname/2019/04/04/