Skip to content
Advertisement

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

Here’s my models

class Robot(models.Model):
    robot = models.CharField(max_length=100)
    short_Description = models.CharField(max_length=200)
    status = models.CharField(max_length=20)
    parameter = models.CharField(max_length=200)
    jenkins_job = models.CharField(max_length=100, default='JenkinsJobName')
    jenkins_token = models.CharField(max_length=100, default='JenkinsToken')
    jenkins_build = models.CharField(max_length=10, default=0)
    jenkins_build_status = models.CharField(max_length=20, default="Never Run")
    def __str__(self):
        return self.robot


class jenkinsHistory(models.Model):
    robotName = models.ForeignKey(Robot, on_delete=models.CASCADE, blank=True, null=True)
    jenkinsBuildNumber = models.CharField(max_length=100,blank=True)
    jenkinsBuildStatus = models.CharField(max_length=200,blank=True)
    errorMsg = models.CharField(max_length=500,blank=True)
    Param = models.CharField(max_length=500,blank=True, null=True)
    def __str__(self):
        return self.robotName

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

def Run_data(Request,id):
    if Request.method == 'POST':
        pi = Robot.objects.get(pk=id)
        hist = jenkinsHistory(robotName= pi,jenkinsBuildStatus='Jenkins-Running')
        hist.save()

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

def Robot_History(Request,id):
    fm = list(jenkinsHistory.objects.values('id','robotName','jenkinsBuildNumber','jenkinsBuildStatus','errorMsg','Param').filter(robotName=id))
    print("hello",fm)
    rob = Robot.objects.all()
    return render(Request, 'hello/robotHistory.html',{'jenkinsHistory': fm,'robot': rob})

and here’s my html

{% for hist in jenkinsHistory %}
                <tbody>
                    <tr>
                        <td>{{hist.id}}</td>
                        <td>{{hist.robotName}}</td>
                        <td>{{hist.jenkinsBuildNumber}}</td>
                        <td>{{hist.jenkinsBuildStatus}}</td>
                        <td>{{hist.errorMsg}}</td>
                        <td>{{hist.Param}}</td>
                    </tr>
                </tbody>
                {% endfor %}

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:

jenkinsHistory.objects.values(..., 'robotName__robot',...)

and use that same field in your template:

{{ hist.robotName__robot }}

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