Django FileField with upload_to determined at runtime
This really helped. For a bit more brevity's sake, decided to use lambda in my case:
file = models.FileField(
upload_to=lambda instance, filename: '/'.join(['mymodel', str(instance.pk), filename]),
)
You've probably read the documentation, so here's an easy example to make it make sense:
def content_file_name(instance, filename):
return '/'.join(['content', instance.user.username, filename])
class Content(models.Model):
name = models.CharField(max_length=200)
user = models.ForeignKey(User)
file = models.FileField(upload_to=content_file_name)
As you can see, you don't even need to use the filename given - you could override that in your upload_to callable too if you liked.