0
votes

I want to sum up the total number of votes made for a login in user that has an award going on. An award is having multiple categories and multiple nominations, what am trying to do is show the sum of votes all nominations that is related to an award

model.py

class Award(models.Model):
    admin = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='award_images')
    slug = models.SlugField(max_length=150)
    about_the_award = models.TextField(blank=True, null=True)
    price = models.CharField(max_length=20, choices=PRICE, default='0.5')
    amount = models.DecimalField(default=0.0, max_digits=19, decimal_places=2)
    date = models.DateTimeField(auto_now_add=True)

class Category(models.Model):
    award = models.ForeignKey(Award, on_delete=models.CASCADE)
    category = models.CharField(max_length=100,)
    slug = models.SlugField(max_length=150)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.category


class Nomination(models.Model):
    fullname = models.CharField(max_length=120)
    mominee_ID = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='nominations_images')
    slug = models.SlugField(max_length=150)
    votes = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)


View.py

@method_decorator([login_required], name='dispatch')
class DashboardView(TemplateView):
    template_name = 'admin/Dashboard.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['user_awards'] = Award.objects.filter(Admin=self.request.user).order_by('-date')
        context['number_of_votes'] = Award.objects.filter(Admin=self.request.user, self.catgory.nomination).annotate(Sum('amount'))
        return context



1

1 Answers

0
votes
from django.db.models import Sum

total_votes = Nomination.objects.filter(category__award=your_award).aggregate(total_votes=Sum('votes'))['total_votes']

Note: it's good practice for your field names to start with a lowercase lette.