我有代码:
import django_filters
class BasicFilter(django_filters.FilterSet):
class Meta:
model = MyDataModel
fields = []
class SiteFilter(BasicFilter):
name = django_filters.ModelMultipleChoiceFilter(
queryset=MySiteModel.objects.all(), help_text=''
)
class Meta(ReportFilter.Meta):
fields = ['name']
我需要获取(在SiteFIlter中)只有user = request.user的记录。像这样:
class SiteFilter(BasicFilter):
name = django_filters.ModelMultipleChoiceFilter(
queryset=MySiteModel.objects.filter(user=current_user), help_text=''
)
class Meta(ReportFilter.Meta):
fields = ['name']
我不知道如何从其他地方获得此用户。我试过这个:
class SiteFilter(BasicFilter):
name = django_filters.ModelMultipleChoiceFilter()
def __init__(self, *args, **kwargs):
if "current_user" in kwargs:
name = django_filters.ModelMultipleChoiceFilter(
queryset=MySiteModel.objects.filter(user=kwargs["current_user"]),
help_text=''
)
class Meta(ReportFilter.Meta):
fields = ['name']
当我创建此过滤器时: site_filter = SiteFilter(current_user =“nickname”)
但只有我得到的,就是这样:
__init__() got an unexpected keyword argument 'current_user'
答案 0 :(得分:-1)
看看你的领域。您希望在哪里插入“current_user”?这看起来不合适。我不太了解过滤器,但只是看看你的代码,也许你想要
site_filter = SiteFilter(name="nickname")