Skip to content
Advertisement

How to display questions based on subcategory using Django Rest Framework?

I want to display all questions based on subcategory.

This is my code :

models.py

JavaScript

serializers.py

JavaScript

view.py

JavaScript

/api/subcategory :

JavaScript

/api/questions :

JavaScript

From the data above I have 2 subcategories Mathematics and History and have 3 questions based on the subcategory, 2 questions about mathematics and 1 question about history.

How to display in /api/questions only 2 questions about Mathematics?

I’m still a beginner and need a lot of guidance please help.

Advertisement

Answer

Try with this:

JavaScript

You’ll have to pass the subcategory string in your URL query parameters:

JavaScript

But if you want it to be filtered all the time with just Mathematics:

JavaScript

You could also try to change subcategory field of Subcategory model to just name to represent the Subcategory name.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement