pythonjsondjangodjango-rest-frameworkjson-api

How to allow 'filter' query parameter Django REST Framework JSON API?


I am using Django REST Framework with djangorestframework-jsonapi

When I query with filter[name]=THEOS DRF raise an error into my browser. I tried to query with this URL

http://localhost:8000/api/space_objects/?filter[name]=THEOS

THe other parameter from JSON API I can use it without any problem.

ValidationError at /api/space_objects/
[ErrorDetail(string='invalid filter[name]', code='invalid')]

And this is my DRF JSON API settings DRF JSON API Documentation

REST_FRAMEWORK = {
    'PAGE_SIZE': 10,
    'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
    'DEFAULT_PAGINATION_CLASS':
        'rest_framework_json_api.pagination.JsonApiPageNumberPagination',
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_json_api.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser'
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_json_api.renderers.JSONRenderer',
        # If you're performance testing, you will want to use the browseable API
        # without forms, as the forms can generate their own queries.
        # If performance testing, enable:
        # 'example.utils.BrowsableAPIRendererWithoutForms',
        # Otherwise, to play around with the browseable API, enable:
        'rest_framework_json_api.renderers.BrowsableAPIRenderer'
    ),
    'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
    'DEFAULT_SCHEMA_CLASS': 'rest_framework_json_api.schemas.openapi.AutoSchema',
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework_json_api.filters.QueryParameterValidationFilter',
        'rest_framework_json_api.filters.OrderingFilter',
        'rest_framework_json_api.django_filters.DjangoFilterBackend',
        'rest_framework.filters.SearchFilter',
    ),
    'SEARCH_PARAM': 'filter[search]',
    'TEST_REQUEST_RENDERER_CLASSES': (
        'rest_framework_json_api.renderers.JSONRenderer',
    ),
    'TEST_REQUEST_DEFAULT_FORMAT': 'vnd.api+json'
}

My model

class SpaceObject(models.Model):
  class Meta:
    ordering = ['norad']

  norad = models.IntegerField(primary_key=True)
  object_type = models.CharField(max_length=200, null=True)
  name = models.CharField(max_length=200, null=True)
  period = models.FloatField(null=True)
  inclination = models.FloatField(null=True)
  apogee = models.FloatField(null=True)
  perigee = models.FloatField(null=True)
  rcs_size = models.CharField(max_length=200, null=True)
  tle_1 = models.CharField(max_length=200, null=True)
  tle_2 = models.CharField(max_length=200, null=True)
  last_updated = models.DateTimeField(max_length=6, default=timezone.now)

My serializer

class SpaceObjectSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = SpaceObject
    fields = ['norad', 'object_type', 'name', 'period', 'inclination', 'apogee', 'perigee', 'rcs_size', 'tle_1', 'tle_2', 'last_updated']

My view

class SpaceObjectViewSet(viewsets.ModelViewSet):
  queryset = SpaceObject.objects.all()
  serializer_class = SpaceObjectSerializer
  permission_classes = [permissions.AllowAny]

I tried to run this but still got the problem

pip install djangorestframework-jsonapi['django-filter']

There is the way to fix the problem? I am tried to follow the documentation but not successful.


Solution

  • I hope you are well. First, you need to make sure that you have Django-Filter installed. Check this with the following command.

    pip install 'django-filter'
    

    Next, add the django-filter to INSTALLED_APPS.

    INSTALLED_APPS = [
    
        'rest_framework',
        'rest_framework_json_api',
        'django_filters',
    
    ]
    

    Then, in your View class, you should set the fields and filter properties. Like the example below:

    class SpaceObjectViewSet(viewsets.ModelViewSet):
      queryset = SpaceObject.objects.all()
      serializer_class = SpaceObjectSerializer
      permission_classes = [permissions.AllowAny]
    
      filterset_fields = {
             'name': ('exact', 'icontains', 'iexact', 'contains',),
      }
    

    and you call api for exact filter similar below:

    http://localhost:8000/api/space_objects/?filter[name]=THEOS
    

    and call for icontains similar below:

     http://localhost:8000/api/space_objects/?filter[name.icontains]=THEOS
    

    And remaining similar to icontains.