[英]Django rest framework viewset get_queryset() doesn't return correct queryset
I am working with Django Rest Framework Filter to access my data.我正在使用 Django Rest Framework Filter 来访问我的数据。 I wrote a custom viewset for one of my models to handle logical 'or' operation on some specifics fields for request with url.
我为我的一个模型编写了一个自定义视图集,以处理对某些特定字段的逻辑“或”操作,以便使用 url 请求。
Here is my ViewSet Class (the printing are just for my debug)这是我的 ViewSet 类(打印仅用于我的调试)
class CustomViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = appart.MyModel.objects.order_by('pk')
serializer_class = MyModelSerializer
filter_class = MyModelFilter
def get_queryset(self):
# getting param
query = dict(self.request.query_params)
print(query)
ct_query = query['caracteristique_text__icontains'][0].split('|')
cl_query = query['caracteristique__libelle__in'][0].split(',')
# writting request syntax
ct = 'Q(caracteristique__libelle__in=' + str(cl_query) + ')&('
print(ct)
for value in ct_query:
ct += "Q(caracteristique_text__icontains='" + value + "')|"
ct = ct[0:len(ct) - 1]
ct += ')'
print(ct)
filtre_text = "global filtre; filtre = " + ct
# creating the request
exec(filtre_text)
#running the request
self.queryset = self.queryset.filter(filtre)
print(self.queryset)
return self.queryset # doesn't return what I see when running print(self.queryset)
And MyModelFilter Class :和 MyModelFilter 类:
class MyModelFilter(ModelFilterSet):
class Meta:
model = appart.MyModel
fields = ('id', 'libelle', 'locataire_appart', 'bien_appart',
'adresse', 'loyer_appart', 'caracteristique', 'caracteristique_text',
'date_creation', 'derniere_maj')
this code is working well and do what I want it to do.这段代码运行良好,可以做我想做的事。 He takes the url parameters, create dynamically the request syntax and return the data i'm looking for.
他获取 url 参数,动态创建请求语法并返回我正在寻找的数据。
The only problem is that the return at the end doesn't give the same result that what I printed in my code.唯一的问题是最后的返回结果与我在代码中打印的结果不同。 He still trying to execute a GET request based on the given url and return nothing when the syntax is not following the django_filter rules.
他仍然尝试根据给定的 url 执行 GET 请求,并且在语法不遵循 django_filter 规则时不返回任何内容。
Can anyone help me to prevent this behavior of my viewsets ?任何人都可以帮助我防止我的视图集的这种行为吗?
You need to specify some filters with Django Filters.您需要使用 Django 过滤器指定一些过滤器。 I think you just need
BaseInFilter
: https://django-filter.readthedocs.io/en/master/ref/filters.html#baseinfilter我认为你只需要
BaseInFilter
: https : BaseInFilter
class MyModelFilter(ModelFilterSet):
caracteristique_text = BaseInFilter(field_name='caracteristique_text', lookup_exp='icontains')
libelle = BaseInFilter(field_name='libelle', lookup_exp='in')
class Meta:
model = appart.MyModel
fields = ('id', 'libelle', 'locataire_appart', 'bien_appart',
'adresse', 'loyer_appart', 'caracteristique', 'caracteristique_text',
'date_creation', 'derniere_maj')
Ok so I finally solved my problem, there were no mistake in the code.好的,我终于解决了我的问题,代码中没有错误。 This was due to the
filter_class = MyModelFilter
.这是由于
filter_class = MyModelFilter
。 I just commented on this line and everything started to work.我刚刚评论了这条线,一切都开始工作了。 I honestly don't know why but i would appreciate a comment to this answer if anyone know the reason.
老实说,我不知道为什么,但如果有人知道原因,我将不胜感激对此答案的评论。
I also took a look on the exec
that @schillingt was talking about and here is the result for those trying to do the same thing or having the same problem.我还查看了@schillingt 正在谈论的
exec
,这是那些试图做同样事情或遇到同样问题的人的结果。
class CustomViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
queryset = models.MyModel.objects.order_by('pk')
serializer_class = MyModelSerializer
filter_class = MyModelFilter
def get_queryset(self):
query = dict(self.request.query_params)
ct = self.request.query_params.get('caracteristique_text__icontains', None)
cl = self.request.query_params.get('caracteristique__libelle__in', None)
if ct is not None and cl is not None:
self.filter_class = None
ct_query = query['caracteristique_text__icontains'][0].split(',')
cl_query = query['caracteristique__libelle__in'][0].split(',')
full_query = Q(caracteristique__libelle__in=cl_query)
ct = Q()
for value in ct_query:
ct_part = Q(caracteristique_text__icontains=value)
ct |= ct_part
full_query &= ct
self.queryset = self.queryset.filter(full_query)
return self.queryset
return models.MyModel.objects.order_by('pk')
This post can also be helpful Filter with or condition这篇文章也很有帮助Filter with or condition
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.