我试图从我的模板中的“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
1条答案
按热度按时间bwleehnv1#
我找到了@willem-van-onsem提供的解决方法
views.py
模板