Search code examples
djangotemplatesmodelforeign-keys

How to show the ForeignKey' content in Django template


I am beginner in django, there is a question i am fronting is that how to show the content of ForeignKey item, for example i have a table called company, there is a field "parentcompany" for storage the ForeignKey(UID) with the parentcompany, all the company is created on the this company table. I already been success to create this relationship, but I can not show the parentcompany's name in the template.

enter image description here

I already tried using this {{company.parentcompany_id.companyName}}, but nothing is shown.

enter image description here


Solution

  • It should be:

    company.parentcompany.companyName
    

    No need to use parentcompany_id, Django will automatically retrieve linked object.