首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django:无法使用prefetch_related

Django:无法使用prefetch_related
EN

Stack Overflow用户
提问于 2021-05-20 23:36:31
回答 1查看 28关注 0票数 0

我想要做的是,对于特定的柱面Id,如果发出柱面,则用户名和发布日期将显示在柱面列表中,如下所示

代码语言:javascript
复制
 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,但它没有显示柱面列表中的任何内容:

这是一个模型:

代码语言:javascript
复制
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) 

这是柱面列表视图:

代码语言:javascript
复制
def cylinderListView(request):
    cylinder=Cylinder.objects.all().prefetch_related('issue_set')
    
    return render(request,'entry/cylinderList.html',locals())

这是cylinderList模板:

代码语言:javascript
复制
{% 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吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-21 00:02:33

您需要按如下方式遍历issue_set

代码语言:javascript
复制
{% 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 %}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67623619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档