Search code examples
pythondjango-rest-frameworkpostgisdjango-rest-viewsetsdjango-rest-framework-gis

How do I configure viewsets.ModelViewSet to filter using arguments passed in the body of the request. (Django Rest Framework )


I'm building an API using DJANGO-REST-FRAMEWORK. I'm trying to filter the back end using parameters passed to the body of the request, this is my set up:

views.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)

models.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'

urls.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)),
    

serializer.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')

I want to pass the parameter in the body of the request (privacy concerns) i.e. body {"id": ["TX", "CA"]}

I'm getting this error:

 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'

since I'm fairly new working with DRF:

1- is this a good approach? am I using the right view classes (viewsets?)?

2- what's the best (recommended way) to filter the models passing values taken from the body of the request?

Note: I'm working with geo-spatial data, a PostgreSQL-PostGIS Back-end . I'm also using rest_framework_gis

sample code would be appreciated, especially recommended code for urls.py and views.py

thanks!


Solution

  • In your urls.py, you have configured something wrong:

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

    The view you want is already included in the line:

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

    by default the endpoint is states/get_by_id/. If you want to the endpoint to be states/q, you configure it in @action decorator.

    @action(detail=False, url_path='q')