[Answered ]-Django – get object stored in Foreign Key

1👍

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)

Leave a comment