从Django模型在模板中显示数据

ztmd8pv5  于 2023-06-07  发布在  Go
关注(0)|答案(1)|浏览(159)

我试图从我的模板中的“junction”模型clientvul访问数据。

models.py

from django.db import models
from django.contrib import admin

class client(models.Model):
    client= models.CharField(max_length=50,primary_key=True)
    user= models.CharField(max_length=100)
    vuls = models.ManyToManyField(
        'vul',
        through='clientvul',
        related_name='client'
    )
    
 
class vul(models.Model):
    vid=models.IntegerField(primary_key=True)
    cvelist=models.CharField(max_length=50)
    cvsscore=models.FloatField(max_length=5)
    serverity=models.CharField(max_length=25)
    title=models.CharField(max_length=1000)
    summary=models.CharField(max_length=1000)

class clientvul(models.Model):
    
    client= models.ForeignKey(client, on_delete=models.CASCADE)
    vid=models.ForeignKey(vul, on_delete=models.CASCADE)
    path=models.CharField(max_length=1000)
    isactive=models.BooleanField(default=True)
    class Meta:
        constraints = [
            models.UniqueConstraint(
               fields=['client', 'vid'], name='unique_migration_host_combination' 
            )
        ]


admin.site.register(client)
admin.site.register(vul)
admin.site.register(clientvul)

views.py

def index(request):
    qs = client.objects.prefetch_related('vuls')
    return render(request, 'browseDB.html', {'data':qs})

模板

{% for row in data %}
      <table class="table">
        <thead class="table-dark">
          <tr>
            <th scope="col">Hostname</th>
            <th scope="col">User</th>
          </tr>
        </thead>
        <tbody>
            <td class="Hostname">{{ row.client }}</a></td>
            <td class="User">{{ row.user }}</td>
          </tbody>
        </table>
        <table class="table">
              <thead>
                <tr>
                  <th scope="col">Title</th>
                  <th scope="col">Severity</th>
                  <th scope="col">CVE</th>
                  <th scope="col">Path</th>
                </tr>
              </thead>
              <tbody>
                {% for subitem in row.vuls.all %}
                <tr>
                  <td class="Title" >{{ subitem.title }}</td>
                  <td class="Severity">{{ subitem.serverity }}</td>
                  <td class="CVE" >{{ subitem.cvelist }}</td>
                  <td class="Path" >{{ subitem.path }}</td>

                </tr>
                {% endfor %}
              </tbody>
            </table>
            <br>
            {% endfor %}

问题是<td class="Path" >{{ subitem.path }}</td>保持为空。我不知道如何从连接模型clientvul引用相关路径。
相关问题:Django prefetch_related with 3 not directly related Models

bwleehnv

bwleehnv1#

我找到了@willem-van-onsem提供的解决方法

views.py

def index(request):
    qs = client.objects.prefetch_related(Prefetch('clientvul_set', clientvul.objects.select_related('VID')))
    return render(request, 'browseDB.html', {'data':qs})

模板

{% for row in data %}
      <div class="card">
        <div class="card-body">
      <table class="table">
        <thead class="table-dark">
          <tr>
            <th scope="col">client</th>
            <th scope="col">User</th>
          </tr>
        </thead>
        <tbody>
            <td class="client">{{ row.client }}</a></td>
            <td class="User">{{ row.user }}</td>
          </tbody>
        </table>
        <table class="table table-bordered">
              <thead>
                <tr>
                  <th scope="col">Title</th>
                  <th scope="col">Severity</th>
                  <th scope="col">CVE</th>
                  <th scope="col">Path</th>
                </tr>
              </thead>
              <tbody>
                {% for subitem in row.clientvul_set.all  %}
                <tr>
                  <td class="Title" >{{ subitem.vid.title }}</td>
                 
                    {% if "Critical" == subitem.vid.serverity %}
                      <td class="Severity"><span class="badge bg-dark">Critical</span></td>
                    {% endif %}
                    {% if subitem.vid.serverity == "High" %}
                      <td class="Severity"><span class="badge bg-danger">High</span></td>
                    {% endif %}
                    {% if subitem.vid.serverity == "Medium" %}
                      <td class="Severity"><span class="badge bg-warning text-dark">Medium</span></td>
                    {% endif %}
                  <td class="CVE" >
                    <a href=https://www.cve.org/CVERecord?id={{ subitem.VID.CVELIST }}>{{ subitem.VID.CVELIST }}</a>
                  </td>
                  <td class="Path" >{{ subitem.path }}</td>

                </tr>
                {% endfor %}
              </tbody>
            </table>
           
          </div>
        </div>
        <br>
            {% endfor %}

相关问题