简体   繁体   中英

Django: Unable to use prefetch_related

What I want to do is that for a particular Cylinder Id, if cylinder is issued then the user name and the issue date will be displayed in cylinder List, like this

 cylinderId | date  | type | status |availability|    issuedate     |  userName | returndate |
        
      1     |  11/11|  co2 |   fill |   available|  12/11(if exists)|     xyz   | 13/11(if exists)

so for that i used prefetch_related but it is not displaying anything in cylinder List:-

here is model:-

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) 

here is cylinder list view:-

def cylinderListView(request):
    cylinder=Cylinder.objects.all().prefetch_related('issue_set')
    
    return render(request,'entry/cylinderList.html',locals())

here is cylinderList template:-

{% 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 %

}

I m unable to figure out that what im missing, Is I m using prefetch_related?

You need to cycle through the issue_set like so:

{% 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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM