python - Trouble with django upload_to callable -
i'm working on uploading file dynamic path based on model instance. here sample code: class test(models.model): def _upload_to(instance, filename): return '/'.join([instance.username.name, filename]) username = models.foreignkey(usermodel) #defined elsewhere file = models.filefield(upload_to=_upload_to) and test it: >>> t = test(file="myfile.txt") >>> t.save() but doesn't seem put in right path, still get: >>> t.file.url /media/myfile.txt when should be /media/someusername/myfile.txt what wrong in example? your test isn't testing right thing. upload_to callable called when upload file, name implies, not when save model. need try actual upload: perhaps try through admin site. when do, run problems because python think function instance method, because have defined inside class. you'll need take outside of class, or decorate @staticmethod .