无法组合不同的查询集并在 Django 中呈现它们

Can't combine different query sets and render them in django

所以我必须在 html 模板上呈现一个 table,它结合了来自 2 个不同基本模型的列。但是 table doent 似乎渲染得很好。来自其他 table 的条目显示在不同的行中。

here is the screenshot of the table that has rendered

-为此,我使用了 itttools 中的链方法。不知道我哪里搞砸了。

Models.py:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='profile_pics')
    dob = models.DateField()
    email = models.EmailField(max_length=50, unique=True)



class Placement(models.Model):
    student = models.OneToOneField(User, on_delete=models.CASCADE)

    company = models.CharField(max_length=40)
    position = models.CharField(max_length=40)
    city = models.CharField( max_length=40)
    bond = models.CharField(  max_length=40)
    ctc = models.CharField( max_length=40)

我的Views.py:

def mentorintern(request):
    table = Placement.objects.all()
    name_student = Profile.objects.all()
    in_table = list(chain(table, name_student))

    return render(request, 'mentorinternship.html', {'in_table': in_table }) 

HTML 模板用于呈现 table:

<table class="table align-items-center table-flush table-hover" id="dataTableHover">
                  <thead class="thead-light">
                    <tr>
                      <th>Name of Student</th>
                      <th>Company</th>
                      <th>Position</th>
                      <th>City</th>
                      <th>CTC</th>
                      <th>Bond</th>
                    </tr>
                  </thead>

                  <tbody>
                    {% for std in in_table %}
                    <tr>

                      <td>{{std.first_name}}</td>
                      <td>{{std.company}}</td>
                      <td>{{std.position}}</td>
                      <td>{{std.city}}</td>
                      <td>{{std.ctc}}</td>
                      <td>{{std.bond}}</td>
                    </tr>
                    {% endfor %} 


                  </tbody>
                </table>

您尝试执行的操作比组合两个查询集更容易实现。由于您有一个 OneToOneField 用户,您可以像这样直接访问用户详细信息:{{std.student.first_name}}} 在 for 循环内。现在,如果我明白你想要什么,你可能需要一个 Profile table in Placement table 的外键。通过这种方式,您可以访问 {{std.profile.first_name}} 的个人资料和 {{std.profile.user.first_name}} 的用户 table。