[英]Django: How to get most recent object from different model?
我正在嘗試顯示以下 output,其中 Cylinder、Issue 和 Return 不同 model,我期望的視圖是柱面表,其中僅顯示最近創建的 issue 和 return 條目,例如:
cylinderId=100 在問題表和返回表中有兩個條目,但在圓柱表中僅顯示最近創建的條目,即:-
cyId | createdAt | issuedDate | username | ReturnDate
100 | 5may,13:00| 6may,14:00 | anyone | 7may,15:00
這是模型:-
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)
class Return(models.Model):
fill=[
('Fill','fill'),
('Empty','empty'),
('refill','Refill')
]
ava=[
('yes','YES'),
('no','NO')
]
cylinder=models.ForeignKey('Cylinder',on_delete=models.CASCADE)
availability=models.CharField(max_length=20,choices=ava)
status=models.CharField(max_length=10,choices=fill)
returnDate=models.DateTimeField(default=timezone.now)
def save(self,*args,**kwargs):
if not self.pk:
if self.cylinder.Availability=='Issued':
if self.availability=='YES' or self.availability=='yes':
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Availability='Available')
if self.status=='empty' or self.status=='Empty':
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Status='Empty')
else:
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Availability='Unavailable')
if self.status=='refill' or self.status=='Refill':
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Status='Refill')
if self.status=='empty' or self.status=='Empty':
Cylinder.objects.filter(cylinderId=self.cylinder.cylinderId).update(Status='Empty')
super().save(*args,**kwargs)
def __str__(self):
return str(self.cylinder)
看法:-
def cylinderListView(request):
cylinder=Cylinder.objects.pefetch_related().order_by('-EntryDate')
return render(request,'entry/cylinderList.html',locals())
模板圓柱列表.html:-
{%for cy in cylinder %}
<tr bgcolor="#e6f0ff" align="center">
<td align="center" height="10"
width="50"><a style="border-width: 0px" 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>
{% if cy.issue_set.all%}
{% 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 %}
{% else %}
<td align="center" height="10"
width="50">-</td>
<td align="center" height="10"
width="50">-</td>
{% endif%}
{% if cy.return_set.all%}
{% for return in cy.return_set.all %}
<td align="center" height="10"
width="50">{{return.returnDate}}</td>
{% endfor %}
{% else %}
<td align="center" height="10"
width="50">-</td>
{% endif%}
</tr>
{% endfor %}
</tbody>
{% else %}
這是我期待的觀點:-
對於這個結果我該怎么辦? 請幫忙:)
您可以使用latest()方法
Cylinder.objects.latest('EntryDate')
這將根據CreatedAt
字段在表中返回最新的 Cylinder object。
如果要列出最新的氣缸,請使用
Cylinder.objects.all().order_by('-EntryDate')
只需將它們查詢為Cylinder.objects.order_by('-pk')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.