Garrett
Garrett

Reputation: 1792

Django aggregate avg price by product category

I have this view:

def MainStatsView(request):
    leads = Leads.objects.all()
    university = University.objects.all()
    price = Leads.objects.aggregate(Avg('price'))


    context = {
            'leads':leads,
            'university':university,
            'price':price,
            }

    return render(request,'stats/stats_main.html',context)

I am trying to get the average price of properties (the product) that generated leads at each university (the category). I am having trouble finding the avg price based on university. I tried using a for loop but that doesn't seem to do the trick.

Also here is my loop:

{% for uni in university %}
    <tr>
        <td>{{ uni.u_name }}</td>
        <td>{{ price }}</td>
        <td>{{ lead.school }}</td>
        <td>{{ lead.featured }}</td>
        <td>{{ lead.price }}</td>
    </tr>
{% endfor %}

The Leads model:

class Leads(models.Model):
    listing = models.CharField(max_length=100,default='Listing')
    timestamp = models.DateTimeField(auto_now_add=True)
    company = models.CharField(max_length=50,default='Company')
    school = models.CharField(max_length=100,default='School')
    featured = models.BooleanField(default=False)
    price = models.IntegerField(blank=False,default=500)
    student_name = models.CharField(max_length=50,default='Name')
    student_phone = models.CharField(max_length=50,default='Phone')
    student_email = models.EmailField(max_length=100,default='[email protected]')
    party_size = models.IntegerField(max_length=1,default=1)

    def __str__(self):
        return self.school

The University model:

class University(models.Model):
    u_date = models.DateTimeField(auto_now_add=True)
    u_name = models.CharField(max_length=100,blank=False)
    u_slug = models.CharField(max_length=100,blank=True)
    name_initials = models.CharField(max_length=5,blank=False)
    address = models.CharField(max_length=100,blank=False)
    color1 = models.CharField(max_length=7,blank=False)
    color2 = models.CharField(max_length=7,blank=False)
    color3 = models.CharField(max_length=7,blank=False)
    u_logo = models.ImageField(upload_to='university_logos',blank=False)
    banner = models.ImageField(upload_to='university_banners',blank=False)
    u_homepage_pic = models.ImageField(default='',upload_to='university_homepage_pic',blank=False)

    def __str__(self):
        return self.u_name

    def save(self, *args, **kwargs):
        self.u_slug = slugify(self.u_name)
        super(University, self).save(*args, **kwargs)

I'm a little all over the place here so that is why it looks overly messy. Here is an image of the result. It's giving me the avg on the price of all properties that have generated leads rather than for one school specifically (which is what I want).

enter image description here

Upvotes: 1

Views: 394

Answers (1)

VMatić
VMatić

Reputation: 996

In Django you can generate aggregates over an entire queryset using aggregate() or an independent summary for each object using annotate(). Here is the documentation.

In your case, it seems you want to annotate the average lead__price to each university object:

university = University.objects.all().annotate(Avg('lead__price'))

Note you may have to adjust the lead__price reference so that it matches your data model, which you haven't provided.

Upvotes: 1

Related Questions