I made an APIView of a django function
Views.py
class TakenQuizListViewAPI(APIView):
def get(self, request, *args, **kwargs):
queryset = self.request.user.supplier.taken_quizzes.select_related('quiz', 'quiz__truck_type').order_by(
'quiz__posted_on')
query = suppliertakenquizSerializer(queryset, many=True).data
return Response(query)
and it returns data like this:
{
"id": 5,
"score": 0.0,
"date": "2019-08-20T13:31:15.156691",
"least_bid": 99,
"confirmed": "Not Confirmed",
"supplier": 32,
"quiz": 16
},
How can I get all the details of the quiz in the API ??
Expected Output:
{
"id": 5,
"score": 0.0,
"date": "2019-08-20T13:31:15.156691",
"least_bid": 99,
"confirmed": "Not Confirmed",
"supplier": 32,
"quiz": { "foo": "",
"bar": ""
}
},
serializer:
class suppliertakenquizSerializer(serializers.ModelSerializer):
class Meta:
model = TakenQuiz
fields = "__all__"
Model.py:
class TakenQuiz(models.Model):
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE, related_name='taken_quizzes')
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name='taken_quizzes')
score = models.FloatField()
date = models.DateTimeField(auto_now_add=True)
least_bid = models.IntegerField(default=0)
confirmed = models.CharField(max_length=100, default='Not Confirmed')
UPDATE What I tried:
I updated the serializer as such that when it gets the quiz ID it should return the quiz data but I am not sure if this is the right thing
class suppliertakenquizSerializer(serializers.ModelSerializer):
quiz = serializers.SerializerMethodField()
def get_items(self, obj):
try:
serializer = createrfqSerializer(Quiz.objects.get(pk=int(obj.)))
quiz_data = serializer.data
except ItemBatch.DoesNotExist:
pass
return quiz_data
class Meta:
model = TakenQuiz
fields = "__all__"