Я пытался свободно использовать rest_framework_filters. Поскольку я строю проект, я не могу понять, что я делаю неправильно, это было почти 2 дня, и разочарование продолжает расти. Что-то не так с моим импортом в rest_framework_filters.backend. Пожалуйста, помогите, я потерялся.
Вот мой класс models.py
from django.db import models
class Manufacturer(models.Model):
name = models.CharField(max_length=128)
created_at = models.DateField(auto_now_add=True)
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=8,decimal_places=2)
description = models.TextField()
release_date = models.DateField()
manufacturer = models.ForeignKey(Manufacturer, related_name='manufacturer', on_delete=models.CASCADE)
Это мой класс filters.py
import rest_framework_filters as filters
from .models import Product
class ProductFilter(filters.FilterSet):
class Meta:
model = Product
fields = {'name':['exact','icontains','in','startswith']}
Это мой класс views.py
from .filters import ProductFilter
from .serializers import ManufacturerSerializer
from .serializers import ProductSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import AllowAny
from rest_framework_filters.backends import DjangoFilterBackend
from base.models import Manufacturer
from base.models import Product
class ManufacturerViewset(ModelViewSet):
permission_classes = (AllowAny,)
serializer_class = ManufacturerSerializer
queryset = Manufacturer.objects.all()
lookup_field = 'pk'
class ProductViewSet(ModelViewSet):
permission_classes = (AllowAny,)
serializer_class =ProductSerializer
queryset = Product.objects.all()
lookup_field = 'pk'
filter_backends = (DjangoFilterBackend,)
filter_class = ProductFilter
def get_queryset(self):
return self.queryset.filter(manufacturer__id=self.kwargs.get('manufacturer_pk'))
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': (
'rest_framework_filters.backends.DjangoFilterBackend',
),
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
'rest_framework.permissions.AllowAny'
]
}
Это ошибка
Internal Server Error: /manufacturers/7/products/
Traceback (most recent call last):
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/viewsets.py", line 114, in view
return self.dispatch(request, *args, **kwargs)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/views.py", line 497, in dispatch
response = self.handle_exception(exc)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/views.py", line 457, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/views.py", line 468, in raise_uncaught_exception
raise exc
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch
response = handler(request, *args, **kwargs)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/mixins.py", line 38, in list
queryset = self.filter_queryset(self.get_queryset())
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework/generics.py", line 150, in filter_queryset
queryset = backend().filter_queryset(self.request, queryset, self)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework_filters/backends.py", line 34, in filter_queryset
return super(DjangoFilterBackend, self).filter_queryset(request, queryset, view)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django_filters/rest_framework/backends.py", line 96, in filter_queryset
return filterset.qs
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/rest_framework_filters/filterset.py", line 266, in qs
qs = super(FilterSet, self).qs
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django_filters/filterset.py", line 237, in qs
qs = self.filter_queryset(qs)
File "/home/asif/drive1/learn/filter/env/lib/python3.6/site-packages/django_filters/filterset.py", line 224, in filter_queryset
queryset = self.filters[name].filter(queryset, value)
KeyError: 'name'
[07/Apr/2020 11:42:44] "GET /manufacturers/7/products/ HTTP/1.1" 500 18002