1👍
✅
You can’t do that there, because there is no user at the time the class definition is executed. Instead, set upload_to
to a callable, which will be called with the instance and the filename, and returns the full path including filename:
def content_file_name(instance, filename):
return os.path.join('media', instance.user.username, filename)
class Image(models.Model):
...
content = models.FileField(upload_to=content_file_name)
Source:stackexchange.com