'How to get foreign key detailed values based on id in html template in django

In Models.py

class Interview(models.Model):
    Current_Date          = models.DateField(auto_now_add=True )
    User                  = models.ForeignKey(User,on_delete=models.CASCADE)
    Recuirement           = models.ForeignKey(Client_Requirement,on_delete=models.CASCADE)
    Candidate             = models.ForeignKey(Candidate, on_delete=models.CASCADE)
    Interviewer           = models.ForeignKey(Interviewer, on_delete=models.CASCADE)
    Interview_Mode        = models.ForeignKey(Interview_Mode, on_delete=models.CASCADE)
    Date                  = models.DateField()
    Time                  = models.TimeField()
    Interview_status      = models.ForeignKey(Interview_status, on_delete=models.CASCADE)
    Interview_Location    = models.CharField(max_length=30)
    Comments              = models.TextField(max_length=1000)
    Update_Date           = models.DateTimeField(auto_now=True)
    def __str__(self):
        return str(self.Interviewer)

in Views.py

def BD(request):  
    Interview_data = Interview.objects.all()
    context={'Interview_data':Interview_data}
    return render(request,'Sub/BD.html', context)
                    

How i can get all the data in Interview table and also individual details of Recuirement, Candidate table in modal bootstrap 5 In for loop tag in django



Solution 1:[1]

<div class="row">
    <div class="row">
        <h1>Graphs</h1>
    </div>
    <div class="row">
        <div class="col-lg-12" style="padding: 20px 60px 0 60px;">
            <table class="table table-hover table-bordered">  
                <thead>
                  <tr class="table-dark"> 
                    <th scope="col">Id</th>
                    <th scope="col">Created_on</th>
                    <th scope="col">Recruiter</th>
                    <th scope="col">Requirement</th>
                    <th scope="col">Candidate</th>
                    <th scope="col">Interview_Mode</th>
                    <th scope="col">Data&Time_Interview</th>
                    <th scope="col">Interview_Status</th>
                    <th scope="col">Interview_Location</th>
                    <th scope="col">Comments</th>
                    <th scope="col">Last_updated</th>
                    <th scope="col">Actions</th>
                  </tr>
                </thead>
                <tbody>
                {% for interview in Interview_data %}
                  <tr>
                    <th scope="row">{{interview.id}}</th>
                    <th scope="row">{{interview.Current_Date}}</th>
                    <th scope="row">{{interview.User}}</th>
                    <td> 
                      <a href="{% url 'Openings_show' %}" style="border-radius: 30px;" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#Requirement">{{interview.Recuirement}}</a>                      
                        <div class="modal fade" id="Requirement" tabindex="-1" aria-labelledby="requirement" aria-hidden="true">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="requirement">Requirement Details</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                              </div>
                              <div class="modal-body">  
                                  <table class="table">
                                    <tbody>
                                      <tr>
                                        <th scope="row">ID</th>
                                        <td>{{ interview.Recuirement.id }}</td> 
                                      </tr>
                                      <tr>
                                        <th scope="row">Company Name</th>
                                        <td>{{ interview.Recuirement.Client_Info }}</td> 
                                      </tr>
                                      <tr>
                                        <th scope="row">Position</th>
                                        <td>{{ interview.Recuirement.Position }}</td> 
                                      </tr>
                                      <tr>
                                        <th scope="row">Department</th> 
                                        <td>{{ interview.Recuirement.Department }}</td>
                                      </tr> 
                                      <tr>
                                        <th scope="row">Experience</th> 
                                        <td>{{ interview.Recuirement.From_Year }} Years {{ interview.Recuirement.To_Year }} Months</td>
                                      </tr>
                                      <tr>
                                        <th scope="row">Notice Period</th> 
                                        <td>{{ interview.Recuirement.NoticePeriod }}</td>
                                      </tr>
                                      <tr>
                                        <th scope="row">Salary Package</th> 
                                        <td>{{ interview.Recuirement.Salary_package}}</td>
                                      </tr>
                                      <tr>
                                        <th scope="row">Location</th> 
                                        <td>{{ interview.Recuirement.Location }}</td>
                                      </tr>
                                      <tr>
                                        <th scope="row">No of Openings</th> 
                                        <td>{{ interview.Recuirement.No_Of_Openings }}</td>
                                      </tr>
                                      <tr>
                                        <th scope="row">Job Description</th> 
                                        <td>{{ interview.Recuirement.Job_Description }}</td>
                                      </tr>
                                    </tbody>
                                  </table> 
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                              </div>
                            </div>
                          </div>
                        </div>
                    </td>
                    <td><button type="button" style="border-radius: 30px;" class="btn  btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#Candidate">
                        {{interview.Candidate}}
                    </button> 
                    <div class="modal fade" id="Candidate" tabindex="-1" aria-labelledby="candidate" aria-hidden="true">
                      <div class="modal-dialog">
                        <div class="modal-content">
                          <div class="modal-header">
                            <h5 class="modal-title" id="candidate">Candidate Details</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                          </div>
                          <div class="modal-body">
                            <table class="table">
                              <tbody>
                                <tr>
                                  <th scope="row">id</th>
                                  <td>{{ interview.Candidate.id }}</td> 
                                </tr>
                                <tr>
                                  <th scope="row">Current_Date</th>
                                  <td>{{ interview.Candidate.Current_Date }}</td> 
                                </tr>
                                <tr>
                                  <th scope="row">Candidate_Name</th>
                                  <td>{{ interview.Candidate.Candidate_name }}</td> 
                                </tr>
                                <tr>
                                  <th scope="row">Client</th>
                                  <td>{{ interview.Candidate.Client_Info }}</td> 
                                </tr>
                                <tr>
                                  <th scope="row">Qualification</th> 
                                  <td>{{ interview.Candidate.Qualification }}</td>
                                </tr> 
                                <tr>
                                  <th scope="row">Experience</th> 
                                  <td>{{ interview.Candidate.Year }} Years {{ interview.Candidate.Months }} Months</td>
                                </tr>
                                <tr>
                                  <th scope="row">Current_Salary</th> 
                                  <td>{{ interview.Candidate.Current_Salary }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Current_Location</th> 
                                  <td>{{ interview.Candidate.Current_Location}}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Expected_Salary</th> 
                                  <td>{{ interview.Candidate.Expected_Salary }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Preferred_Location</th> 
                                  <td>{{ interview.Candidate.Preferred_Location }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Languages_Known</th> 
                                  <td>{{ interview.Candidate.Languages_Known }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Marital_Status</th> 
                                  <td>{{ interview.Candidate.Marital_Status }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Candidate_Notice_Period</th> 
                                  <td>{{ interview.Candidate.Candidate_Notice_Period }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Email</th> 
                                  <td>{{ interview.Candidate.Email }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Primary_Contact_No</th> 
                                  <td>{{ interview.Candidate.Primary_Contact_No }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Secondary_Contact_No</th> 
                                  <td>{{ interview.Candidate.Secondary_Contact_No }}</td>
                                </tr> 
                                <tr>
                                  <th scope="row">Comments</th> 
                                  <td>{{ interview.Candidate.Comments }}</td>
                                </tr>
                                <tr>
                                  <th scope="row">Update_Date</th>
                                  <td>{{ interview.Candidate.Update_Date }}</td> 
                                </tr>
                              </tbody>
                            </table>
                          </div>
                          <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                          </div>
                        </div>
                      </div>
                    </div></td>
                    <td>{{interview.Interview_Mode}}</td>
                    <td>{{interview.Date}}-{{interview.Time}}</td>
                    <span class="badge rounded-pill bg-primary">Primary</span>
                    <td>{{interview.Interview_status}}</td>
                    
                    <td>{{i.Interview_Location}}</td> 
                    <td>{{interview.Comments}}</td>
                    <td>{{interview.Update_Date}}</td>
                    <td><a href="" class="btn btn-sm btn-outline-warning" style="border-radius: 50px;">Edit</a></td>
                  </tr> 
                {% endfor %}
                </tbody>
              </table>
        </div>
    </div>
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s