I’m using get_context_data in a view to show a sum of elements in a table.
This is working very well.
But if i’m using different user account i have no chance to only get the sum of elements which are belonging to the current user.
Does anybody have an idea to get this?
Thx and kind regards
Michael
We’re going to need to see a lot more detail to be able to help you with this.
Please post the complete code for the models and the view that you’re currently using.
When posting multiple lines of code, please enclose it between two lines consisting only of three backtick - ` characters. So you’ll have one line that is only ```, followed by your lines of code, followed by another line of ```. That will ensure your code remains formatted. (Be sure to use the backtick - ` and not the apostrophe - '.)
1 Like
You should be able to get the user from the request object of the get_context_data function.
user = self.request.user
# get sum of elements...
If this isn’t what you were asking for, please update your original post with more details
Hi, thanks a lot for your help in advance.
Short: I have a wine cellar list with multiple fields.
Most is working fine.
Now, on top of the view there u can see the full amount of bottles. This works fine. But now we have different users, and they all are see the full sum over all the wines in table.
The view for every user to only see their own data is workin fine.
here is the list View over all:
class WinesView(LoginRequiredMixin, generic.ListView):
model = Wine
template_name = 'wine/wine_list.html'
# Show nmbr. of bottles
def get_context_data(self, *args, **kwargs):
context = super(WinesView, self).get_context_data(*args, **kwargs)
context['bottles_sum'] = Wine.objects.all().aggregate(Sum('nmbrbottles'))['nmbrbottles__sum']
context['wines_sum'] = Wine.objects.count()
return context
# Filter user data only
def get_queryset(self):
query_set = super().get_queryset()
return query_set.filter(owner=self.request.user)
Show nmbr. of bottles shows the total sum and number of different wines.
If you’re talking about modifying these two lines to be filtered
by the user
:
context['bottles_sum'] = Wine.objects.all().aggregate(Sum('nmbrbottles'))['nmbrbottles__sum']
context['wines_sum'] = Wine.objects.count()
you can do something like this:
context['bottles_sum'] = Wine.objects.filter(owner=self.request.user).aggregate(Sum('nmbrbottles'))['nmbrbottles__sum']
context['wines_sum'] = Wine.objects.filter(owner=self.request.user).count()
2 Likes
Hi Ken
Great, it works! Thanks very much for your help!
I spent a lot of time on that, but couldn’t manage the objects.filter.
Cool, thank you.
i’m using get_context_data too and have some issues
class TaskList(LoginRequiredMixin, ListView):
model = Task
context_object_name = ‘tasks’
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['tasks'] = context['tasks'].filter(user=self.request.user)
return context
but unfortunately cant render any data any tips?
I would suggest you open a new topic for this discussion, posting the all of the view, model and template involved.
When you do, and when you post code or templates here, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template), then another line of ```.
1 Like