Django Summernote plugin upload image by userid

I am using the Summernote plugin for Django and my target is to allow users to upload media inside the server. At the moment files are organized (by default) in a folder named with the upload date. Something like:

  • "ProjectName/media/django-summernote/2024-10-11/989d2f98-ad3c-47d6-9c07-e5f6d0c731e6.png"
  • "ProjectName/media/django-summernote/2024-10-17/13d646b8-d7cd-4e04-a76a-804a1ee0d090.jpg".

Is it possible to change the path and include the user_id in the path? Something like

  • "ProjectName/media/django-summernote/User100/989d2f98-ad3c-47d6-9c07-e5f6d0c731e6.png"
  • "ProjectName/media/django-summernote/User200/13d646b8-d7cd-4e04-a76a-804a1ee0d090.jpg".

What I have done

I made these edits in the settings.py file

# Summernote plugin
def summernote_upload_to(request, filename):
 user = request.user
    
    # Create the dynamic path
 upload_path = f'user_upload/{user}'
    return os.path.join(upload_path)

SUMMERNOTE_CONFIG = {
    'attachment_upload_to': summernote_upload_to,
    'summernote': {
        'attachment_filesize_limit': 200 * 1000 * 1000, # specify the file size
        'width': '100%',
        'height': '480',
 }
}

but, when I upload an image, I get an error

AttributeError: 'Attachment' object has no attribute 'user'

Вернуться на верх