0

我正在使用 DJANGO-REST-FRAMEWORK 构建一个 API。我正在尝试使用传递给请求正文的参数来过滤后端,这是我的设置:

视图.py

from .models import UsStatesG
from .serializer import UsStatesSerializer
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action

class StatesViewSet(viewsets.ModelViewSet):

    queryset = UsStatesG.objects.all()
    serializer_class = UsStatesSerializer

    @action(detail=False)
    def get_by_id(self, request):
        body = request.body.decode('utf-8')
        body = json.loads(body)

        state_list = UsStatesG.objects.filter(st_abbr__in=body['id'])
        serializer = self.get_serializer(state_list, many=True)
        return Response(serializer.data)

模型.py

class UsStatesG(models.Model):
    st_fips = models.CharField(primary_key=True, max_length=2, unique=True)
    geom = models.MultiPolygonField(blank=False, null=False)
    objectid = models.BigIntegerField(blank=False, null=False)
    st_name = models.CharField(max_length=50, blank=False, null=False)
    st_abbr = models.CharField(max_length=2, blank=False, null=False, unique=True)

    class Meta:
        managed = False
        db_table = 'US_States_G'

网址.py

from django.contrib import admin
from django.urls import include, path, re_path
from api import views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'states', views.StatesViewSet)
router.register(r'states/q', views.StatesViewSet.get_by_id, basename='UsStatesG')

urlpatterns = [
    path('api/location/v1/', include(router.urls)),
    

序列化程序.py

from rest_framework_gis.serializers import GeoFeatureModelSerializer
from .models import UsStatesG

class UsStatesSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = UsStatesG
        geo_field = "geom"
        fields = ('st_fips', 'st_name', 'st_abbr')
        read_only_fields = ('st_fips', 'st_name', 'st_abbr')

我想在请求的正文中传递参数(隐私问题),即 body {"id": ["TX", "CA"]}

我收到此错误:

 File "C:\GIT_WS\geospatial\api\REST\APIProject\urls.py", line 31, in <module>
    path('api/location/v1/', include(router.urls)),
  File "C:\ProgramData\Anaconda3\envs\cgeo\lib\site-packages\rest_framework\routers.py", line 78, in urls
    self._urls = self.get_urls()
  File "C:\ProgramData\Anaconda3\envs\cgeo\lib\site-packages\rest_framework\routers.py", line 339, in get_urls
    urls = super().get_urls()
  File "C:\ProgramData\Anaconda3\envs\cgeo\lib\site-packages\rest_framework\routers.py", line 237, in get_urls
    routes = self.get_routes(viewset)
  File "C:\ProgramData\Anaconda3\envs\cgeo\lib\site-packages\rest_framework\routers.py", line 153, in get_routes
    extra_actions = viewset.get_extra_actions()
AttributeError: 'function' object has no attribute 'get_extra_actions'

因为我是使用 DRF 的新手:

1-这是一个好方法吗?我是否使用了正确的视图类(视图集?)?

2-过滤模型传递从请求正文中获取的值的最佳(推荐方式)是什么?

注意:我正在使用 PostgreSQL-PostGIS Back-end 地理空间数据。我也在使用rest_framework_gis

示例代码将不胜感激,尤其是 urls.py 和 views.py 的推荐代码

谢谢!

4

1 回答 1

1

在您的 urls.py 中,您配置了一些错误:

router.register(r'states/q', views.StatesViewSet.get_by_id, basename='UsStatesG')

您想要的视图已包含在以下行中:

router.register(r'states', views.StatesViewSet)

默认情况下,端点是states/get_by_id/. 如果您希望端点是,您可以在装饰器states/q中配置它。@action

@action(detail=False, url_path='q')
于 2020-08-28T06:42:18.590 回答