I found a way!

Am 04.10.2008 um 18:28 schrieb Ulises:
> Anyway, I still think that the heavy lifting should be done in a view
> and that the template should be in charge of just displaying what has
> been sent to it.

And I did it in the view...

models.py:
def get_monthly_expenses(self, user):
     expenses = Expense.objects.filter(date__month =  
datetime.date.today().month).filter(user__username =  
user.username).filter(category = self)
     return expenses

def get_monthly_total(self, user):
     monthly_total = 0.0
     expenses = self.get_monthly_expenses(user)
     for expense in expenses:
         monthly_total += expense.expense
     return monthly_total

views.py:
categories = []
cats = Category.objects.all()
for cat in cats:
     expenses = cat.get_monthly_expenses(request.user)
     total = cat.get_monthly_total(request.user)
     categories.append({'category': cat, 'expenses': expenses,  
'total': total})

template:
{% if categories %}
        {% for category in categories %}
                {% if category.expenses %}
                        <p>{{ category.category }}</p>
                        {% for expense in category.expenses %}
                                <p>{{ expense.date }}: {{ expense.expense }}</p>
                        {% endfor %}
                        <p>{{ category.total }}</p>
                {% endif %}
        {% endfor %}
{% endif %}

Thanks!
-benjamin


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to