- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Django REST 项目,我在其中使用了来自 django-rest-framework-filters github link 的 DjangoFilterBackend .
这个后端适用于我的一些具有 ForeignKey 和 ManyToMany 关系的模型,但是,它不适用于其他一些模型并且总是给我一个 AttributeError: 'NoneType' object has no attribute 'verbose_name '
。我的模型:
from django.db import models
from authuser.models import AbstractUser, BaseUserManager
from corporation.models import Corporation
class Organization(AbstractUser):
company = models.ForeignKey(Corporation, verbose_name='company',
related_name='organizations',
null=True, on_delete=models.SET_NULL)
contact_name = models.CharField(max_length=50, verbose_name='contact_name', default='')
contact_phone = models.CharField(max_length=50, null=True, verbose_name='contact_phone')
contact_email = models.EmailField(max_length=50, verbose_name='contact_email', default='')
about_us = models.TextField(null=True, verbose_name='about_us')
info_completed = models.BooleanField(default=False, verbose_name='info_completed')
objects = BaseUserManager()
class Meta:
ordering = ('date_joined',)
我的观点:
class OrganizationDetailView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = OrganizationSerializer
queryset = Organization.objects.all()
class OrganizationListCreateView(generics.ListCreateAPIView):
queryset = Organization.objects.all()
permission_classes = ()
def get_serializer_class(self):
if self.request.method == "POST":
return CreateOrganizationSerializer
return OrganizationSerializer
filter_backends = (
rest_backends.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter,
)
filter_fields = (
'contact_name',
)
filter_class = OrganizationFilter
search_fields = (
# 'company',
'contact_name',
'contact_email', 'contact_phone',
'description',
)
ordering = ('date_joined')
我的过滤器:
class OrganizationFilter(filters.FilterSet):
company = filters.RelatedFilter(CorporationFilter, name='company')
description = filters.AllLookupsFilter(name='description')
contact_name = filters.AllLookupsFilter(name='contact_name')
contact_phone = filters.AllLookupsFilter(name='contact_phone')
contact_email = filters.AllLookupsFilter(name='contact_email')
class Meta:
model = Organization
fields = (
'company',
'description', 'contact_name', 'contact_phone',
'contact_email',
)
我的序列化器:
class OrganizationSerializer(serializers.ModelSerializer):
company = serializers.PrimaryKeyRelatedField(queryset=Corporation.objects.all())
class Meta:
model = Organization
fields = ('id', 'company',
'contact_name', 'contact_email',
'contact_phone', 'info_completed')
CRUD 操作很好。它只是在列出组织时给我属性错误:
Creating test database for alias 'default'...
E.........
======================================================================
ERROR: test_list_organizations (organization.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "F:\landi-django\organization\tests.py", line 73, in test_list_organizati
ons
response = client.get('/organizations?contact_name=Tingtao')
File "F:\landi-django\env\lib\site-packages\rest_framework\test.py", line 160,
in get
response = super(APIClient, self).get(path, data=data, **extra)
File "F:\landi-django\env\lib\site-packages\rest_framework\test.py", line 86,
in get
return self.generic('GET', path, **r)
File "F:\landi-django\env\lib\site-packages\rest_framework\compat.py", line 18
9, in generic
return self.request(**r)
File "F:\landi-django\env\lib\site-packages\rest_framework\test.py", line 157,
in request
return super(APIClient, self).request(**kwargs)
File "F:\landi-django\env\lib\site-packages\rest_framework\test.py", line 109,
in request
request = super(APIRequestFactory, self).request(**kwargs)
File "F:\landi-django\env\lib\site-packages\django\test\client.py", line 440,
in request
six.reraise(*exc_info)
File "F:\landi-django\env\lib\site-packages\django\core\handlers\base.py", lin
e 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\landi-django\env\lib\site-packages\django\views\decorators\csrf.py",
line 57, in wrapped_view
return view_func(*args, **kwargs)
File "F:\landi-django\env\lib\site-packages\django\views\generic\base.py", lin
e 69, in view
return self.dispatch(request, *args, **kwargs)
File "F:\landi-django\env\lib\site-packages\rest_framework\views.py", line 407
, in dispatch
response = self.handle_exception(exc)
File "F:\landi-django\env\lib\site-packages\rest_framework\views.py", line 404
, in dispatch
response = handler(request, *args, **kwargs)
File "F:\landi-django\env\lib\site-packages\rest_framework\generics.py", line
311, in get
return self.list(request, *args, **kwargs)
File "F:\landi-django\env\lib\site-packages\rest_framework\mixins.py", line 40
, in list
instance = self.filter_queryset(self.get_queryset())
File "F:\landi-django\env\lib\site-packages\rest_framework\generics.py", line
144, in filter_queryset
queryset = backend().filter_queryset(self.request, queryset, self)
File "F:\landi-django\env\lib\site-packages\rest_framework_filters\backends.py
", line 45, in filter_queryset
_filter = filter_class(request.QUERY_PARAMS, queryset=queryset)
File "F:\landi-django\env\lib\site-packages\rest_framework_filters\filterset.p
y", line 61, in __init__
f = self.filter_for_field(field, filter_.name)
File "F:\landi-django\env\lib\site-packages\django_filters\filterset.py", line
400, in filter_for_field
'label': capfirst(f.verbose_name),
AttributeError: 'NoneType' object has no attribute 'verbose_name'
----------------------------------------------------------------------
我还尝试在 django_filters\filterset.py 中注释第 400 行,但它给了我更多错误。非常感谢您的帮助!
最佳答案
在您的 OrganizationFilter
上,您正在定义一个 description
字段,该字段可用于向下过滤到 Organization
对象。 description
字段在 Organization
模型中不存在,因此 django-filter 无法根据该字段过滤查询集。您似乎有一个 about_us
字段,我猜这就是您打算使用的。
您可以通过将 OrganizationFilter
上的字段重命名为 about_us
或设置 description 上的
字段为 name
来解决此问题about_us
,因此 django-filter 使用该字段。
class OrganizationFilter(filters.FilterSet):
company = filters.RelatedFilter(CorporationFilter, name='company')
about_us = filters.AllLookupsFilter(name='about_us')
contact_name = filters.AllLookupsFilter(name='contact_name')
contact_phone = filters.AllLookupsFilter(name='contact_phone')
contact_email = filters.AllLookupsFilter(name='contact_email')
class Meta:
model = Organization
fields = (
'company',
'description', 'contact_name', 'contact_phone',
'contact_email',
)
这些更改中的任何一个都应该使其正常工作而不会引发错误。
关于python - DjangoFilterBackend 不适用于我的模型(manytomany),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247920/
我想在 django 中创建 View 集,它将采用带有查询参数的 url 并根据查询参数进行过滤。我对文档的最大问题是它不能证明它们是白痴。以 django Rest Framework 文档 ht
我有一个 Django REST 项目,我在其中使用了来自 django-rest-framework-filters github link 的 DjangoFilterBackend . 这个后端
升级到Django REST框架3.7后,我收到以下错误 File "/home/jpg/Projects/django2x/sample/views.py", line 24, in
我将 DjangoFilterBackend 用于所有字段: class EntitiesViewSet(viewsets.ModelViewSet): queryset = Entity.o
我正在使用 django-filter 但我收到以下错误,即“模块”对象没有属性“DjangoFilterBackend” 我在下面发送我的代码: 模型.py from django.db i
我的课: class InvoicePeriodViewSet(viewsets.ModelViewSet): permission_classes = ( permissio
我创建了一个 AssetsFilter 类: from django_filters import Filter from django_filters import rest_framework a
我是一名优秀的程序员,十分优秀!