Django-Filter FilterSet 只显示用户生成的对象
Django-Filter FilterSet Show Only User Generated Objects
我正在使用 django-filter 表单,它正在过滤 'associated_portfolios' 的所有对象我怎样才能让它只向用户显示他们创建的对象?
错误信息:
'StatsFilter' object has no attribute 'fields'
Filters.py
class StatsFilter(django_filters.FilterSet):
associated_portfolios = django_filters.ModelMultipleChoiceFilter(queryset=associated_portfolios)
class Meta:
model = Trade
fields = ['type', 'asset', 'symbol', 'broker', 'patterns', 'associated_portfolios']
def __init__(self, request, *args, **kwargs):
super(StatsFilter, self).__init__(*args, **kwargs)
self.fields['associated_portfolios'].queryset = Trade.objects.filter(user=request.user)]
views.py
class StatsView(LoginRequiredMixin, FilterView):
model = Trade
template_name = 'dashboard/stats.html'
filterset_class = StatsFilter
def get_context_data(self, **kwargs):
filter = StatsFilter(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_users_trades'] = Trade.objects.get_users_trades('tj3admin')
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
return context
啊,现在我想起来了:将 ModelMultipleChoiceFilter
的查询集参数设置为接受请求的可调用对象,因为它是唯一的参数:
def portfolio_filtered_queryset(request):
return Trade.objects.filter(user=request.user)
class StatsFilter(django_filters.FilterSet):
associated_portfolios = django_filters.ModelMultipleChoiceFilter(queryset=porfolio_filtered_queryset)
The view:
class StatsView(LoginRequiredMixin, FilterView):
model = Trade
template_name = 'dashboard/stats.html'
filterset_class = StatsFilter
def get_context_data(self, **kwargs):
# Must pass in request!
filter = StatsFilter(self.request.GET, queryset=self.get_queryset(), request=self.request)
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_users_trades'] = Trade.objects.get_users_trades('tj3admin')
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
return context
在django_filters.filters.QuerySetRequestMixin.get_request()
中,请求实例是从父级获得的。但我在 django_filters.filterset.BaseFilterSet
中看不到任何逻辑或试图通过其他方式获取请求的具体内容。因此,如果您希望使用 QuerySetRequestMixin,则必须将请求传递给 StatsFilter。
我正在使用 django-filter 表单,它正在过滤 'associated_portfolios' 的所有对象我怎样才能让它只向用户显示他们创建的对象?
错误信息:
'StatsFilter' object has no attribute 'fields'
Filters.py
class StatsFilter(django_filters.FilterSet):
associated_portfolios = django_filters.ModelMultipleChoiceFilter(queryset=associated_portfolios)
class Meta:
model = Trade
fields = ['type', 'asset', 'symbol', 'broker', 'patterns', 'associated_portfolios']
def __init__(self, request, *args, **kwargs):
super(StatsFilter, self).__init__(*args, **kwargs)
self.fields['associated_portfolios'].queryset = Trade.objects.filter(user=request.user)]
views.py
class StatsView(LoginRequiredMixin, FilterView):
model = Trade
template_name = 'dashboard/stats.html'
filterset_class = StatsFilter
def get_context_data(self, **kwargs):
filter = StatsFilter(self.request.GET, queryset=self.get_queryset())
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_users_trades'] = Trade.objects.get_users_trades('tj3admin')
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
return context
啊,现在我想起来了:将 ModelMultipleChoiceFilter
的查询集参数设置为接受请求的可调用对象,因为它是唯一的参数:
def portfolio_filtered_queryset(request):
return Trade.objects.filter(user=request.user)
class StatsFilter(django_filters.FilterSet):
associated_portfolios = django_filters.ModelMultipleChoiceFilter(queryset=porfolio_filtered_queryset)
The view:
class StatsView(LoginRequiredMixin, FilterView):
model = Trade
template_name = 'dashboard/stats.html'
filterset_class = StatsFilter
def get_context_data(self, **kwargs):
# Must pass in request!
filter = StatsFilter(self.request.GET, queryset=self.get_queryset(), request=self.request)
context = super().get_context_data(**kwargs)
context['filter'] = filter
context['get_users_trades'] = Trade.objects.get_users_trades('tj3admin')
context['get_largest_winning_trade'] = filter.qs.aggregate(max_value=Max('profit_loss_value_fees'))['max_value']
return context
在django_filters.filters.QuerySetRequestMixin.get_request()
中,请求实例是从父级获得的。但我在 django_filters.filterset.BaseFilterSet
中看不到任何逻辑或试图通过其他方式获取请求的具体内容。因此,如果您希望使用 QuerySetRequestMixin,则必须将请求传递给 StatsFilter。