我的模特:
class Rate(models.Model):
building = models.ForeignKey(Building, verbose_name="Objekt")
year = models.IntegerField("Jahr")
monthly_rate = models.DecimalField("Monatsrate", max_digits=8, decimal_places=2)
payed = models.DecimalField("Tatsächlich Bezahlt (Brutto)", max_digits=8, decimal_places=2, blank=True, null=True)
class META:
get_latest_by = ['year']
def __unicode__(self):
return unicode(self.monthly_rate)
def get_absolute_url(self):
return "/rates/detail/%i" % self.id
def yearly(self):
return self.monthly_rate*12
我需要建筑物的最新费率对象。
我试过了:
rate = building.rate_set.latest('year')
和
rate = Rate.objects.filter(building=building).latest('year')
和.....
每次我收到错误:DoesNotExist:费率匹配查询不存在
如何编写正确的查询? 需要更多信息吗?
答案 0 :(得分:0)
尝试:
rate_list_by latest = Rate.objects.filter(building=building).order_by('-pk')
按年份尝试:
rate_list_by_latest = Rate.objects.filter(building=building).order_by('-year')
最新费率对象:
rate_latest_object = rate_list_by_latest[0]