0

I tried to create a question filtering function dependent on values from request: question_type and situation. If both predicates provided, I want Questions filterd by both values. If only one of them - I want Questions filtered by provided filter only.

However, no matter what filter values I send in question_type or situation arguments, all questions are returned, no filtering.

models.py

class Question(models.Model):
    TYPES_CHOICES = [
        ("LAWYER","LAWYER"),
        ("REPAIRMAN","REPAIRMAN"),
        
    ]
    SITUATION_CHOICES = [
        ("in","IN"),
        ("wait","WAIT"),
        ("closed","CLOSED")
    ]
    question_type = models.CharField(max_length=255,choices=TYPES_CHOICES,default="LAWYER")
    title = models.CharField(max_length=255)
    description = models.TextField()
    image = models.ImageField(upload_to="question_images/",blank=True,null=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    situation = models.CharField(max_length=20,choices=SITUATION_CHOICES,default="in")

views.py

class TypeView(ListAPIView):
    serializer_class = QuestionSerializer

    def get_queryset(self):
        qs = Question.objects.all()
        question_type = self.request.query_params.get('question_type')
        situation = self.request.query_params.get('situation')
        
        if question_type and situation:
            qs = qs.filter(question_type=question_type, situation=situation)
        elif question_type:
            qs = qs.filter(question_type=question_type)
        elif situation:
            qs = qs.filter(situation=situation)
            
        return qs

urls.py

    path('search/<str:question_type>/<str:situation>/', views.TypeView.as_view(), name='filtered-question'),
4
  • What didn't work? Commented Apr 22, 2023 at 11:03
  • No matter what value I sent as question type or situation, all questions are sent, there is no filtering. Commented Apr 22, 2023 at 12:13
  • 1
    query_params are these things: search?question_type=a&situation=b%20c - arguments after ?. You're not using query params thus .query_params is always empty. Params from url_pattern are stored in kwargs see the docs Commented Apr 22, 2023 at 13:29
  • Does this answer your question? Capture parameters in django-rest-framework Commented Apr 22, 2023 at 13:33

1 Answer 1

0

The issue here is that you are using URL path parameters instead of query parameters in your URL configuration.

Update your urls.py as follows.

path('search/', views.TypeView.as_view(), name='filtered-question'),

In your TypeView class in views.py, you are already reading the query parameters correctly using self.request.query_params.get(). So, you don't need to change anything in the get_queryset() method.

You should now be able to make requests such as these:

/search/?question_type=LAWYER&situation=in
/search/?question_type=LAWYER
/search/?situation=in
Sign up to request clarification or add additional context in comments.

Comments

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.