python - Django:无法使用 prefetch_related
问题描述
我想要做的是对于特定的 Cylinder Id ,如果发布了圆柱体,那么用户名和发布日期将显示在圆柱体列表中,如下所示
cylinderId | date | type | status |availability| issuedate | userName | returndate |
1 | 11/11| co2 | fill | available| 12/11(if exists)| xyz | 13/11(if exists)
所以为此我使用了 prefetch_related 但它没有在圆柱列表中显示任何内容:-
这是模型:-
class Cylinder(models.Model):
stachoice=[
('Fill','fill'),
('Empty','empty')
]
substachoice=[
('Available','available'),
('Unavailable','unavailable'),
('Issued','issued')
]
cylinderId=models.CharField(max_length=50,primary_key=True,null=False)
gasName=models.CharField(max_length=200)
cylinderSize=models.CharField(max_length=30)
Status=models.CharField(max_length=40,choices=stachoice,default='fill')
Availability=models.CharField(max_length=40,choices=substachoice,default="Available")
EntryDate=models.DateTimeField(default=timezone.now)
def get_absolute_url(self):
return reverse('cylinderDetail',args=[(self.cylinderId)])
def __str__(self):
return str(self.cylinderId)
class Issue(models.Model):
cylinder=models.ForeignKey('Cylinder',on_delete=models.CASCADE)
userName=models.CharField(max_length=60,null=False)
issueDate=models.DateTimeField(default=timezone.now)
def save(self,*args,**kwargs):
if not self.pk:
if self.cylinder.Availability=='Available':
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Availability=('Issued'))
super().save(*args,**kwargs)
def __str__(self):
return str(self.userName)
这是气缸列表视图:-
def cylinderListView(request):
cylinder=Cylinder.objects.all().prefetch_related('issue_set')
return render(request,'entry/cylinderList.html',locals())
这是圆柱列表模板:-
{% extends 'base.html'%}
{% block content %}
<div class="alldiv">
<h1 align="center">All Cylinder list</h1>
{% if cylinder %}
<div class='centerstage'>
<div class="post">
<table border="5" cellspacing="5" width="100%" >
<thead>
<tr bgcolor="#99c2ff"align="center">
<th height="50"
width="50">Cylinder Id</th>
<th height="50"
width="50">Date</th>
<th height="50"
width="50">Gas Name</th>
<th height="50"
width="50">Cylinder Size</th>
<th height="50"
width="50">Status</th>
<th height="50"
width="50">Availability</th>
<th height="50"
width="50">Issued Date</th>
<th height="50"
width="50">Customer</th>
<th height="50"
width="50">Return Date</th>
</thead>
<tbody>
{%for cy in cylinder%}
<tr bgcolor="#e6f0ff" align="center">
<td align="center" height="10"
width="50"><a href="{{cy.get_absolute_url}}">{{cy.cylinderId}}<a></td>
<td align="center" height="10"
width="50">{{cy.EntryDate}}</td>
<td align="center" height="10"
width="50">{{cy.gasName}}</td>
<td align="center" height="10"
width="50">
{{cy.cylinderSize}}</td>
<td align="center" height="10"
width="50">
{{cy.Status}}</td>
<td align="center" height="10"
width="50">{{cy.Availability}}</td>
<td align="center" height="10"
width="50">{{cy.issue.issueDate}}</td>
<td align="center" height="10"
width="50">{{cy.issue.userName}}</td>
</tr>
{% endfor %}
</tbody>
{% else %}
<div>
<h2>No record</h2>
</div>
</table>
</div>
</div>
</div>
{% endif %}
{% endblock %
}
我无法弄清楚我缺少什么,我正在使用 prefetch_related 吗?
解决方案
您需要issue_set
像这样循环:
{% for issue in cy.issue_set.all() %}
<td align="center" height="10" width="50">{{ issue.issueDate }}</td>
<td align="center" height="10" width="50">{{ issue.userName }}</td>
{% endfor %}
推荐阅读
- python - 使用 python 库 mysql.connector 将 csv 插入 MySQL 数据库
- ios - 如何在swift 4中滚动到tableView的顶部?
- video - 如何在结束前 2.24 秒分割多个视频
- tizen - Tizen 本机服务未获得传感器特权
- html - WebStorm 无法识别 Ionic HTML 元素/标签 - 例如“找不到名称 'ion'”
- bash - 在脚本中使用“tail -f | grep”来简单地显示输出
- xamarin.forms - 如何使用 UrhoSharp 在 Xamarin 中加载自定义字体?
- c++ - 整数除以双精度数
- flutter - 使用火焰/音频播放器时,如何停止来自 AudioCache 的音频?
- javascript - 在 GatsbyJS 中将 iframe 高度设置为 scrollHeight