S
S
StynuBlizz2017-04-09 11:39:01
Django
StynuBlizz, 2017-04-09 11:39:01

How to generate path to save file dynamically, DJANGO?

Here is the code

def upload_video(request):

      v = Video(video = request.FILES['videoFile'])
      v.safe()

class Video(models.Model):
     video = model.FileField(upload_to="/path/")

The problem is that the path to save the file needs to be generated dynamically, how can this be done

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-04-09
@StynuBlizz

At least there is this
https://docs.djangoproject.com/en/dev/ref/models/f...
username/time/

import os
import datetime


def user_directory_path(instance, filename):
    # file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
    return os.path.join(
               instance.user.name, 
               datetime.datetime.now().strftime('%Y_%m_%d__%H_%M'), 
               filename)


class MyModel(models.Model):
    upload = models.FileField(upload_to=user_directory_path)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question