![]() So then I tried to append it to a list instead, like this: a = īut then I get the list of attempts for every query, instead of the attempt number for each query. If I do it like this, I always get the last value for attempt_number (because the loop overwrites the value). String Fields: CharField, EmailField, and RegexField are the. I'm getting the right queryset, but I'm not sure how to get only the relevant value for every query. Serializer Fields Boolean Fields: A field that stores True or False values in boolean format. Since serializer tries to access fields for model, you can describe properties by setting property in your model or define custom SerializerMethodField, all this could possibly use all fields of your model. It's working fine, except for my attemptnumber field. No you cannot achieve that by using builtin features of django and restframework. My serializer for the ListAPI looks as follows: class MyQuizListSerializer(serializers.ModelSerializer):Īttempt = serializers.SerializerMethodField() I'm trying to get my ListAPI to show various fields of my Quiz (and related) models. User = models.ForeignKey(User, on_delete=models.CASCADE)Ĭorrect_answers = models.IntegerField(default=0)Ĭompleted = models.BooleanField(default=False)Īttempt_number = models.PositiveIntegerField(default=0) Is_correct = models.BooleanField('Correct answer', default=False) Question = models.ForeignKey(Question, on_delete=models.CASCADE)Īnswer = models.CharField(max_length=100) Label = models.CharField(max_length=1000) Quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE) Questions_count = models.IntegerField(default=0) My model setup is as follows: class Quiz(models.Model): Users can take every quiz as many times as they want, and I want to show the queryset for each attempt, since the score etc. Django Rest Framework - related fields as nested objects for GET and pk for POST only. ![]() I'm getting the right queryset, but I'm not sure how to get only the relevant value for every query. Django Rest Framework : Nested Serializer Dynamic Model Fields. It's working fine, except for my attempt_number field. I'm trying to get my ListAPI to show various fields of my Quiz (and related) models.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |