0
我的models.py如下:Django的查詢集有額外的過濾器
class Prescription(models.Model):
date_prescribed = models.DateTimeField()
doctor = models.ForeignKey(Doctor)
pharmacy = models.ForeignKey(Pharmacy)
class Doctor(models.Model):
name = models.CharField(max_length=150)
age = models.PositiveSmallIntegerField()
class Pharmacy(models.Model):
name = models.CharField(max_length=150)
status = models.CharField()
我現在需要找到一個月爲期六個月今天的分組一個月作爲起始月追溯到六所有處方month.I試圖打殼了一下,得到這個查詢工作:在一個視圖中使用不工作的時候
end_date = timezone.now()
start_date = end_date - relativedelta(months=6)
qs = (Prescription.objects.extra(select={'month': connection.ops.date_trunc_sql('month', 'date_prescribed')})
.filter(date_prescribed__range=(start_date,end_date))
.annotate(Count('id'))
.order_by('month'))
然而,同樣的:
class PrescriptionTrendListView(generics.ListAPIView):
queryset = Prescription.objects.all()
serializer_class = LineGraphSerializer
def get_queryset(self):
end_date = timezone.now()
start_date = end_date - relativedelta(months=6)
truncate_date = connection.ops.date_trunc_sql('month', 'date_prescribed')
qs = super(PrescriptionTrendListView,self).get_queryset.extra(select={'month': truncate_date})
return qs.filter(date_prescribed__range=(start_date, end_date)).annotate(pk_count=Count('pk')).order_by('month')
我得到錯誤,指出「功能對象沒有額外的屬性」。 我在做什麼錯?
aaaargh ......我會花時間打破了我的頭,以身影,out..thanks爲提那.. – Amistad