![](/img/trans.png)
[英]AttributeError: 'QuerySet' object has no attribute 'tags'
[英]AttributeError: 'QuerySet' object has no attribute 'objects' when using count and sum
我在顯示列表總數時遇到問題。 我想計算下面我的 list_value 變量的總數,但它似乎返回了一個錯誤。
AttributeError: 'QuerySet' object has no attribute 'objects' when using count
視圖.py
@login_required(login_url='log_permission')
def data_table(request):
if request.method=='POST':
province = request.POST['province']
municipality = request.POST['municipality']
list_value =
Person.objects.filter(province=province,municipality=municipality)
final_value = list_value.objects.count() //here is the problem
total =dats.objects.aggregate(Sum('amount'))
print(final_value)
print(total)
return render(request, 'data_table.html')
楷模
class Person(models.Model):
covid_id = models.CharField(max_length=50)
firstname = models.CharField(max_length=50)
middle = models.CharField(max_length=50,blank=True, null=True)
lastname = models.CharField(max_length=50,blank=True)
extension = models.CharField(max_length=50,blank=True, null=True)
province = models.CharField(max_length=50)
municipality = models.CharField(max_length=50)
barangay = models.CharField(max_length=50)
remarks = models.CharField(max_length=50)
amount = models.CharField(max_length=50)
remarks_miray = models.CharField(max_length=50)
count = models.CharField(max_length=50)
您不需要調用object
,只需調用count()
方法
count_value = list_value.count()
list_value 已經是一個 QuerySet 而不是 Django 模型。 因此 .objects 不可用。 你可以遍歷list_value。 更多信息: https : //docs.djangoproject.com/en/3.1/topics/db/queries/#retrieving-objects
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.