2

I have this problem while using the Django Rest Framework. I am trying to do a get request using patient id. Now, lets say I look for patientid=6, the api returns results with patientid=6 ,patientid=26 and any ids that contain the number '6'. It looks like it searches for a substring.I want to make it work such that patient id=6 returns only the patientid results with id =6

serializers.py

class Radiologypdfserializerdata(serializers.ModelSerializer):

    class Meta:
        model = models.Radiologypdf
        fields = (
            'patientid',
            'testinfo',
            'clinicalindication',
            'attendingdoctor',
            'patientname',
            'age',
            'mobilenumber',
            'sex',
            'email',
            'doctorsname',
            'doctorsregistrationnumber',
            'clinicname',
            'doctorstelno',
            'createdtime',
            'radiology_id',
            'created',

        )

api.py

class RadiologypdfViewSet(viewsets.ModelViewSet):
    """ViewSet for the radiology class"""

    queryset = models.Radiologypdf.objects.all()
    serializer_class = serializers.Radiologypdfserializerdata
    permission_classes = [permissions.IsAuthenticated]

    filter_backends = (filters.SearchFilter,)
    search_fields = ('patientid','radiology_id')

3 Answers 3

2

You can use django-filter library for this.

Just install it with:

pip install django-filter

And use in your viewset like this:

from django_filters.rest_framework import DjangoFilterBackend

class RadiologypdfViewSet(viewsets.ModelViewSet):
    """ViewSet for the radiology class"""

    queryset = models.Radiologypdf.objects.all()
    serializer_class = serializers.Radiologypdfserializerdata
    permission_classes = [permissions.IsAuthenticated]

    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('patientid','radiology_id')
Sign up to request clarification or add additional context in comments.

Comments

0

Excerpt from the docs: http://www.django-rest-framework.org/api-guide/filtering/#searchfilter

The search behavior may be restricted by prepending various characters to the search_fields.

  • '^' Starts-with search.
  • '=' Exact matches.
  • '@' Full-text search. (Currently only supported Django's MySQL backend.)
  • '$' Regex search.

For example change your code to the following:

`search_fields = ('=patientid','radiology_id')`

Comments

0

Since you are already using SearchFilter, you can just prepend "=" to your filter fields. In this case your filter fields would be:

filter_fields = ('=patientid','=radiology_id')

According to the docs:

The search behavior may be restricted by prepending various characters to the search_fields.

  • '^' Starts-with search.
  • '=' Exact matches.
  • '@' Full-text search. (Currently only supported Django's PostgreSQL backend.)
  • '$' Regex search.

1 Comment

filter_fields do not support = ^ $ ... only search_fields allows you to search based on these options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.