Skip to content
Advertisement

How to get String value instead of number in foreign key field in django?

Here’s my models

JavaScript

I have assign data in jenkinsHistory table from Robot table. here’s the code that how i assign the data

JavaScript

now i want to show that data in a table in my UI. so that i have written this view

JavaScript

and here’s my html

JavaScript

But when i got the data the foreign key field coming as a id not string enter image description here

but in my django admin it is coming as a string only enter image description here

how to solve that issue?

Advertisement

Answer

You can use robotName__robot in your values call:

JavaScript

and use that same field in your template:

JavaScript

While at it, I suggest you change jenkinsHistory‘s robotName field to just robot, as this pertains to the foreign key (or Robot object) and not just the name. This will help make your code less confusing and more readable.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement