1👍
✅
With FileField you can put a [function on upload_to][1]
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to
1👍
Your approach is flawed either way because whatever you pass into upload_to
is going to be called once. Even if user.username
worked, you have to remember that it’s only calculated when the class is defined.
You’ll want to define a custom upload_to
function to pass to the field.
def custom_upload_to(instance, filename):
return '{instance.user.username}/'.format(instance=instance)
myfield = models.FileField(upload_to=custom_upload_to)
- [Answered ]-Prevent multiple hits to database in django templates
- [Answered ]-Django CSRF and Sessions
- [Answered ]-Django-haystack with elastic search not building index for newly created objects
- [Answered ]-Strange issue at subclassing?
Source:stackexchange.com