I want to display all questions based on subcategory.
This is my code :
models.py
JavaScript
x
24
24
1
class Subcategory(models.Model):
2
subcategory_id = models.BigAutoField(primary_key=True)
3
subcategory = models.CharField(max_length=40)
4
5
class Meta:
6
managed = True
7
db_table = 'subcategory'
8
9
def __str__(self):
10
return self.subcategory
11
12
class Question(models.Model):
13
question_id = models.BigAutoField(primary_key=True)
14
subcategory = models.ForeignKey('Subcategory', models.DO_NOTHING, default=None)
15
question = models.TextField()
16
answer = models.CharField(max_length=255)
17
18
class Meta:
19
managed = True
20
db_table = 'question'
21
22
def __str__(self):
23
return self.question
24
serializers.py
JavaScript
1
16
16
1
class SubcategorySerializer(serializers.ModelSerializer):
2
3
class Meta:
4
model = Subcategory
5
fields = ('subcategory_id',
6
'subcategory')
7
8
class QuestionSerializer(serializers.ModelSerializer):
9
10
class Meta:
11
model = Question
12
fields = ('question_id',
13
'subcategory',
14
'question',
15
'answer')
16
view.py
JavaScript
1
24
24
1
@api_view(['GET', 'POST', 'DELETE'])
2
def question_list(request):
3
# GET list of question, POST a new question, DELETE all question
4
if request.method == 'GET':
5
questions = Question.objects.all()
6
7
question = request.GET.get('question', None)
8
if question is not None:
9
questions = questions.filter(question__icontains=question)
10
11
questions_serializer = QuestionSerializer(questions, many=True)
12
return JsonResponse(questions_serializer.data, safe=False)
13
# 'safe=False' for objects serialization
14
elif request.method == 'POST':
15
question_data = JSONParser().parse(request)
16
questions_serializer = QuestionSerializer(data=question_data)
17
if questions_serializer.is_valid():
18
questions_serializer.save()
19
return JsonResponse(questions_serializer.data, status=status.HTTP_201_CREATED)
20
return JsonResponse(questions_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
21
elif request.method == 'DELETE':
22
count = Question.objects.all().delete()
23
return JsonResponse({'message': '{} Questions were deleted successfully!'.format(count[0])}, status=status.HTTP_204_NO_CONTENT)
24
/api/subcategory :
JavaScript
1
11
11
1
[
2
{
3
subcategory_id: 1,
4
subcategory: "Mathematics"
5
},
6
{
7
subcategory_id: 2,
8
subcategory: "History"
9
}
10
]
11
/api/questions :
JavaScript
1
21
21
1
[
2
{
3
question_id: 1,
4
subcategory: 1,
5
question: "10 + 10 = ?",
6
answer: "20"
7
},
8
{
9
question_id: 2,
10
subcategory: 1,
11
question: "50 + 50 = ?",
12
answer: "100",
13
},
14
{
15
question_id: 3,
16
subcategory: 2,
17
question: "Who was the first president of the United States?",
18
answer: "George Washington",
19
}
20
]
21
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
1
4
1
subcategory = request.GET.get('subcategory', None)
2
if subcategory is not None:
3
questions = questions.filter(subcategory__subcategory__icontains=subcategory)
4
You’ll have to pass the subcategory string in your URL query parameters:
JavaScript
1
2
1
?subcategory=Mathematics
2
But if you want it to be filtered all the time with just Mathematics
:
JavaScript
1
2
1
questions.filter(subcategory__subcategory='Mathematics')
2
You could also try to change subcategory
field of Subcategory
model to just name
to represent the Subcategory name.