使用Multiselectfield上的django过滤器后端过滤

时间:2019-04-12 06:58:56

标签: django django-rest-framework django-filters

无法使用DjangoFilterBackend进行过滤,其返回NULL。

使用其他属性(例如名称或值)时,效果很好。

__basic_fields =('name',)
filter_fields = __basic_fields

它返回正确的值。

import django_filters
from rest_framework.viewsets import ModelViewSet
from .models import UserDetails
from .serializer import UserDataSerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import ListAPIView
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.pagination import LimitOffsetPagination
class MyDetailViewSet(ModelViewSet):

     queryset = putDetails.objects.all()
     serializer_class = putDataSerializer
     filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
     __basic_fields =('stock',)
     ilter_fields = __basic_fields

“股票”是我的models.py中的Multiselectfield。

我应该以其他方式处理MultiSelect吗?

Traceback Traceback (most recent call last):   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\base.py", line 154, in _get_response
    response = response.render()   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\response.py", line 106, in render
    self.content = self.rendered_content   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 733, in render
    context = self.get_context(data, accepted_media_type, renderer_context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 710, in get_context
    'filter_form': self.get_filter_form(data, view, request),   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 642, in get_filter_form
    html = backend().to_html(request, queryset, view)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django_filters\rest_framework\backends.py", line 105, in to_html
    return template.render(context, request)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py", line 199, in render
    c = self.get_render(context).flatten()   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py", line 118, in get_render
    actual_form.form_html = helper.render_layout(actual_form, node_context, template_pack=self.template_pack)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\helper.py", line 308, in render_layout
    template_pack=template_pack   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 140, in render
    return self.get_rendered_fields(form, form_style, context, template_pack, **kwargs)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 104, in get_rendered_fields
    for field in self.fields   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 104, in <genexpr>
    for field in self.fields   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\utils.py", line 148, in render_field
    html = template.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 940, in render
    bits.append(str(bit))   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\utils\html.py", line 397, in <lambda>
    klass.__str__ = lambda self: mark_safe(klass_str(self))   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\boundfield.py", line 33, in __str__
    return self.as_widget()   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\boundfield.py", line 93, in as_widget
    renderer=self.form.renderer,   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 229, in render
    context = self.get_context(name, value, attrs)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 668, in get_context
    context = super().get_context(name, value, attrs)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 629, in get_context
    context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)   File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 592, in optgroups
    for subvalue, sublabel in choices: ValueError: too many values to unpack (expected 2) [12/Apr/2019 12:52:58] "GET /api/usr/ HTTP/1.1" 500 297265

0 个答案:

没有答案