The first problem, like you pointed, is that you are hard-coding the results in the get_context_data()
. The FilterView
ClassBasedView inherits from the MultipleObjectsMixin mixin of Django, so you should take both into account. I assume from your issues that you are not using the object_list
property in your template - that's where the CBV populates the data.
Basically, in your current view, the flow will go as:
- get the queryset based in the
get_queryset()
method.
- If this doesn't exist, it will use the
queryset
property defined
- In last case, the
model
. <-- This is your case, so it will use Trade.objects.all()
- the FilterSet is applied to the original queryset
- paginate (if you have it configured) and assign the filtered queryset to the
object_list
in the context.
So, some changes you can start trying:
- Use the
object_list
to perform the stats calculation in your get_context_data()
instead of querying for the Trade
model yourself.
If you are always going to filter the results based in the current user, you can use the get_queryset()
method to apply that filter from there, so you would do:
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(user=self.request.user, status='cl')
I would also recommend you to look at Django's built-in data aggregation mechanisms, as it would make all those calculations at DB level. If your start to treat big amounts of data, making all this manual processing is going to be an issue.
I think those points would be a good start!