JefferyLR JefferyLR - 1 year ago 101
Python Question

Django Models Filefiled save according to query ID/pk

I am new to django and sqlite. From the code below i will upload my file to 'documents', since there is an overwrite storage function, if i upload the same file name with another query i will overwrite the old one which i dont wan it to be happend. So what I am thinking is to get the query id or pk as the directory.
Its there anyway to store my upload file via their pk or id?


eg. document/name/test.zip

eg. document/name/test.zip

enter image description here

Answer Source

First define a function

def upload(instance, filename):
    return "document/" + instance.user.name +"/" + filename

And in the filefield of model

upload_to=upload
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download