Django: AttributeError: 'Q' object has no attribute 'count'


Joey Coding

I try to get the count of all attendees. I created the following for loopand query set. However, I always get the error message AttributeError: 'Q' object has no attribute 'count'. Do you have a solution?

# Get count of attendees per ticket and then combine these
tickets = Ticket.objects.filter(event=3)
count = 0
for ticket in tickets:
    new_count = ticket.attendees.filter=Q(
        canceled=False,
        order__status__in=(
            OrderStatus.PAID,
            OrderStatus.PENDING,
            OrderStatus.PARTIALLY_REFUNDED,
            OrderStatus.FREE,
        ),
    ).count()
    count += new_count
print(count)
William Van Anselm

You assign to .filter: Note that what you wrote is .filter=Q(not .filter(Q(...)).

You can solve this problem by using:

tickets = Ticket.objects.filter(event=3)
count = 0
for ticket in tickets:
    new_count = ticket.attendees.filter(
        canceled=False,
        order__status__in=(
            OrderStatus.PAID,
            OrderStatus.PENDING,
            OrderStatus.PARTIALLY_REFUNDED,
            OrderStatus.FREE,
        )
    ).count()
    count += new_count
print(count)

That being said, I think you can handle this more efficiently with a call similar to the following:

Attendee.objects.filter(
    ticket__event=3,
    canceled=False,
    order__status__in=(
        OrderStatus.PAID,
        OrderStatus.PENDING,
        OrderStatus.PARTIALLY_REFUNDED,
        OrderStatus.FREE,
    )
).count()

So here we Attendeeuse ticketfor to count the number of s event=3. This may be more efficient than doing n queries .

Related


Django: AttributeError: 'Q' object has no attribute 'count'

Joey Coding I try to get the count of all attendees. I created the following for loopand query set. However, I always get the error message AttributeError: 'Q' object has no attribute 'count'. Do you have a solution? # Get count of attendees per ticket and the

AttributeError: 'window' object has no attribute 'q'

Vandelay I am trying to append a simple string to a list in an and object. But I guess the self keyword interferes with the pyqt window? How can I fix this? class Window(qt.QMainWindow): def __init__(self, parent=None): super(Window, self).__init__

Django AttributeError ""Object has no attribute""

User 15440172 I'm working on the following/followers feature of the CS50w network. I stick to counting followers: Here is my model: class Following(models.Model): """Tracks the following of a user""" user = models.ForeignKey(User, on_delete=models.CASC

Django: AttributeError: "Object has no attribute"

јон.с I'm trying to convert a property computed in one class to another. And I'm stuck... In the "Reward" class, I need to subtract the "deductible" from the "pledge_level". In the "Pledge" class, I need to subtract "not_taxable" from "Amount" to return "decuc

AttributeError: 'bool' object has no attribute 'count'

Csci319 I am new to Python and I am writing this code below. fileName = input("Enter the file name: ") InputFile = open(fileName, 'r') text=InputFile.readable() sentences = text.count('.') + text.count('?') + \ text.count(':') + text.count(';') +

AttributeError: 'bool' object has no attribute 'count'

Csci319 I am new to Python and I am writing this code below. fileName = input("Enter the file name: ") InputFile = open(fileName, 'r') text=InputFile.readable() sentences = text.count('.') + text.count('?') + \ text.count(':') + text.count(';') +

Django AttributeError 'tuple' object has no attribute 'regex'

Egg white protein I am using django 1.8 and having trouble. I am trying to import tinymce in my project. when i render it AttributeError: tuple 'object has no attribute 'regex' When I remove the url in url.py it is working. Here is my code. url.py from django.

Django AttributeError: 'alias' object has no attribute 'url'

participate and learn I just finished writing my Heroes App model: Here is the models.py file for my hero app: Import models from django.db # Create your models here. class Hero(models.Model): codename = models.CharField(max_length = 30) profilePic = m

Django AttributeError: form object has no attribute '_errors'

Skillset 12345 I am overriding the init method in the form and now it returns the error 'TransactionForm' object has no attribute '_errors' . I hope it will work because I have included super in init , but maybe I don't know how to use it properly. An explanat

Django AttributeError 'tuple' object has no attribute 'regex'

Oyilmaztekin I am using django 1.8 and having trouble. I am trying to import tinymce in my project. when i render it AttributeError: tuple 'object has no attribute 'regex' When I remove the url in url.py it is working. Here is my code. url.py from django.conf.

Django AttributeError model object has no attribute 'filter'

Ian Bertrand I am trying to create my own blog using Django. I have a view to display articles extended from DetailView. To avoid any trouble, I'm trying to categorize articles based on their publication date. The URL of the article is like this (where pk corr

Django - AttributeError: 'str' object has no attribute 'objects'

put up I am a beginner in Python and I am getting an error -AttributeError: 'str' object has no attribute 'objects' models.py has class feedback(models.Model): prid=models.ForeignKey(addproduct,on_delete=models.CASCADE) urid=models.ForeignKey(userreg,on_

AttributeError: 'tuple' object has no attribute 'get' in Django

Jewish I want to add a multiselect field to my project app. This field should allow the user to select multiple options. But I get an error. This part doesn't give any errors before adding the multiselect field. Where is my mistake? my views.py def project_new

AttributeError: 'NoneType' object has no attribute '_meta' Django

Eve 11 I am new to this and I want to change the selected data (food_status) and save it to the selected column (food_status column). but it shows an error For fields in self._meta.concrete_fields: AttributeError: 'NoneType' object has no attribute '_meta' in

Django AttributeError: 'int' object has no attribute 'save'

Samir Tenduka I have 2 model profiles and events. I want to save the number of events created by the user to the user profile so that even if the event is deleted the record remains in the user profile class Profile(models.Model): user = models.OneToOneFie

Django AttributeError 'tuple' object has no attribute 'regex'

Egg white protein I am using django 1.8 and having trouble. I am trying to import tinymce in my project. when i render it AttributeError: tuple 'object has no attribute 'regex' When I remove the url in url.py it is working. Here is my code. url.py from django.