0

I have a problem with show total number of my list. I want to count the total number of my list_value variable below, but it seems it returning an error.

AttributeError: 'QuerySet' object has no attribute 'objects' when using count

views.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')

models

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)

2 Answers 2

1

You don't need to call the object, just call count() method

count_value = list_value.count() 
Sign up to request clarification or add additional context in comments.

2 Comments

How about the sum? I used variable_name.aggregate(Sum('amount')) but it returns output like this {'amount__sum': 167200.0} . I just want the variable 167200.0
Use get() method as variable_name.aggregate(Sum('amount')).get("amount__sum")
0

list_value is already a QuerySet and not a Django model. therefore .objects is unavailable. you can just iterate through list_value. more info here: https://docs.djangoproject.com/en/3.1/topics/db/queries/#retrieving-objects

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.