JefferyLR JefferyLR - 1 year ago 85
Python Question

Django (Models): how to get instance from foreign key

i'm new to django and sqlite. Is that possible i can get the instance.name value from its foreign key that is related to modeldb? I need to save my pdf file under the same directory as the modeldb class. From the above experiment, im got nothing with modelDB.instance / modelDB.instance.name.

enter image description here

Answer Source

move that upload function outside the class.

instance refer as the your class instance. So you can do like this

instance.modelDB.name

citatiions = models.FileField(...., upload_to=upload) #pass your function name here

check this answerlink

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