I am trying to consolidate the field values in Django 3.2 and add them and group them month & year wise, but I am unable to get it work, below is my code.
models.py
class Event(models.Model):
name = models.CharField(max_length=255, null=True)
MONTH = (
('january', 'january'),
('february', 'february'),
('march', 'march'),
('april', 'april'),
('may', 'may'),
('june', 'june'),
('july', 'july'),
('august', 'august'),
('september', 'september'),
('october', 'october'),
('november', 'november'),
('december', 'december'),
)
YEAR = (
('2023', '2023'),
('2024', '2024'),
('2025', '2025'),
('2026', '2026'),
('2027', '2027'),
('2028', '2028'),
('2029', '2029'),
('2030', '2030'),
)
month = models.CharField(max_length=255, null=True, choices=MONTH)
year = models.CharField(max_length=255, null=True, choices=YEAR)
event_price = models.IntegerField(null=True)
def __str__(self):
return self.name
views.py
from django.db.models import Sum
def viewEvent(request):
event = Event.objects.all()
total_event_price = Event.objects.values(sum('event_price))
context = {'event': event, 'total_event_price: total'}
return render(request, 'view_event.html', context)
view_event.html
<div class="card card-body">
<table class="table table-sm">
<tr>
<th>Year</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
<th>Total</th>
</tr>
{% for items in event %}
<tr>
<td>{{ items.year }}</td>
<td>{{ items.month }}</td>
<td>{{ items.total }}</td>
</tr>
I want the entries added as per month wise and another total which adds all month wise as per the year, which is total field. how do I achieve ?