1

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

As robotName is a FK to robot model, when you get this via values(), queryset returns you a 'robot'. Then you are passing that robot object to yout template with robotName name.

So when you try to show robotName in template, it shows you it's id Try robotName__robot:

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

Or you can do it in your template:

<td>{{hist.robotName.robot}}</td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.