'How to filter multiselect query on django drf?

I'm making a request via GET with the following query:

site.org/mymodel?ordering=-created&state=awaiting_review

and I have updated to send with multiple states:

site.org/mymodel?ordering=-created&state=awaiting_review,phone_screened,interview_scheduled,interview_completed,offer_made,hired,rejected

But I have not found a doc in django that specifies how to treat it. So I'm not sure what to do right now. Maybe there is a vanilla option of how to do it.

The following codes are a mock of the current ones. What kind of function of part of django/python should I use to receive a filtered query with multiple values ?

[the frontend is imutable , only the following code is available to be changed (python)]

the current one complains throwing this as response too.

{
  "state": [
    "Select a valid choice. awaiting_review,reviewed,phone_screened,interview_scheduled,interview_completed,offer_made,hired,rejected is not one of the available choices."
  ]
}

Model.py: `

class MyModel():
    STATE_OPTIONS = (
        ("awaiting_review", "awaiting_review"),
        ("reviewed", "reviewed"),
        ("phone_screened", "phone_screened"),
        ("interview_scheduled", "interview_scheduled"),
        ("interview_completed", "interview_completed"),
        ("offer_made", "offer_made"),
        ("hired", "hired"),
        ("rejected", "rejected"),
    )

    state = models.CharField(
        max_length=19, choices=STATE_OPTIONS, default="awaiting_review", null=False
    )

Serializer.py:

class ASerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ( state )
`

view.py: `

class MyModelView(generics.ListAPIView, ProtectedResourceView):
    serializer_class = ASerializer
    filter_fields = ("state")
    ordering_fields = ("state")

    def get_queryset(self):
        return MyModel.objects.filter(user=self.request.user).filter(
        something_thats_not_state = self.request.query_params.get("something_thats_not_state")
        )

` Code Image of how the view actually is



Solution 1:[1]

Close, you can try extending the url, I like to do something like this:

scripts.js


// assuming we are sending a request via ajax:

// create a django like filter:
var filter = JSON.stringify({
  state__in : ['offer_made', 'hired']
});

// add the filter as a parameter to the url:
var url = 'api/model/?filter=' + filter;

// send the request:
$.ajax({
  url : url,
  success : function(response) {
    
    // do something with the response:
    console.log(response)
  }
});

Your filter in js looks just like the django filter, and your url then looks something like:

.../api/model/?filter={'state__in':['offer_made','hired']}

views.py

import json

class ModelView(...):

    ...
    
    def get_queryset(self):

        # unpack the filter:
        filter_string = self.request.GET.get('filter', {})
        filter_dictionary = json.loads(filter_string)

        # filter queryset:
        queryset = self.queryset.filter(**filter_dictionary)

        return queryset

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1