[Django]-Django Create file in MEDIA_ROOT folder and save it to FileField

2👍

Solved this issue. The root cause of the issue due to python cannot open file with relative path. So we can solve this issue in two steps.

  1. Open file from absolute path as below(use absolute path)
    f = open(settings.MEDIA_ROOT + '/text_file/'+ text_file + '.txt','w')
    f.close()
    
  2. then update/save file(use relative path)
    queryset.filter(pk=voice.pk).update(textFile='text_file/' + text_file + '.txt')
    

Hope can help someone who hit similar question.

👤Kumar

Leave a comment