[Fixed]-Django model with FileField — dynamic 'upload_to' argument

27👍

Instead of a string try passing a function:

def generate_filename(self, filename):
    url = "files/users/%s/%s" % (self.user.username, filename)
    return url

class UserFiles(models.Model):
    user = models.OneToOneField(User)
    file = models.FileField(upload_to=generate_filename)

Leave a comment