It sounds like a strange question, but I'll ask it... Is it possible to access the currently logged in user in a model?
This question is related to the following problem: I have the models 'expense' and 'category' to keep track of my monthly expenses: class Category(models.Model): category = models.CharField(max_length=100) slug = models.CharField(max_length=100) class Expense(models.Model): category = models.ForeignKey(Category) date = models.DateField(default=datetime.date.today) description = models.TextField(blank=True) expense = models.FloatField() user = models.ForeignKey(User) I'd now like to have a page where a logged in user can see his expenses for the actual month in the following way: Category1: expense1: 3.5 expense2: 3.5 total: 7.0 Category2: expense1: 2 expense2: 3 total: 5 So I have to limit the query set by user and date (actual month), sort them somehow into the categories and sum up all expenses within one category. I first tried to solve this problem in the template. I queried for expenses and limited it by user and date like this: expenses = Expense.objects.filter(user__username = user.username).filter(date__month = today.month).order_by('category') Then I did this in the template: {% if expenses %} <table> {% for expense in expenses %} {% ifchanged %}<tr><td>{{ expense.category }}</td></tr>{% endifchanged %} <tr> <td>{{ expense.date }}</td> <td>{{ expense.expense }}</td> </tr> {% endfor %} </table> {% endif %} But as you can see there's no sum at the and, and I couldn't think of a way to do this. Second attempt was to do it from within the model. I came up with the following methods, attached to the model 'Category': def get_monthly_expenses(self): expenses = Expense.objects.filter(date__month = datetime.date.today().month) return expenses def get_monthly_total(self): monthly_total = 0.0 expenses = self.get_monthly_expenses() for expense in expenses: monthly_total += expense.expense return monthly_total I would then pass Category.objects.all() to the template. The problem here is that I have no possibility to filter the expenses by logged in user beause I can't access the user from within the model. Accessing the user from within the model seems somehow like a bad idea, but I can't see an other way to do this. -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 -~----------~----~----~----~------~----~------~--~---