http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers

On Mon, Feb 26, 2018 at 7:46 AM, sakthi selvam <sakthipanneer1...@gmail.com>
wrote:

> *models.py*
>
> class Question(models.Model):
>     id = models.UUIDField(primary_key=True, default=uuid.uuid4, 
> editable=False)
>     question = models.TextField(unique=True)
>
>  ....
>
> class Choice(models.Model):
>     id = models.UUIDField(primary_key=True, default=uuid.uuid4, 
> editable=False)
>     question_id = models.ForeignKey(Question, on_delete=models.CASCADE, 
> related_name="choices")
>     choice = models.TextField()
>
>     ...
>
>
> *serializers.py*
>
> class ChoiceSerializer(serializers.ModelSerializer):
>
>     class Meta:
>         model = Choice
>         fields = ('id', 'choice',)
>         read_only_fields = ('id',)
>
>
> class QuestionFormatSerializer(serializers.ModelSerializer):
>     choices = ChoiceSerializer(many=True, read_only=True)
>
>     class Meta:
>         model = Question
>         fields = ('id', 'question', 'choices')
>         read_only_fields = ('id',)
>
> *views.py*
>
> class QuestionViewSet(viewsets.ModelViewSet):
>
>     serializer_class = QuestionFormatSerializer
>
>     queryset = Question.objects.all()
>
>         def get_serializer_class(self):
>
>         serializer_class = self.serializer_class
>
>         if self.request.method == 'POST' and self.request.method == 'PUT':
>             serializer_class = serializer_class(many=True)
>
>         return serializer_class
>
>
> *request data *
>
> *{
>     "question": "In an Information and Communication Technologies (ICTs) 
> context, hardware refers to",
>     "choices": [
>         {
>             "choice": "Physical components of technologies",
>         },
>         {
>             "choice": "Difficult-to-use programmes",
>         },
>         {
>             "choice": "Rare or specialised applications",
>         },
>         {
>             "choice": "Tough or resilient computer components",
>         }
>     ]
> }*
>
>
> Here i'm not able to post the choices data. only the question data is 
> inserted and choices will be empty list. Please Reply ASAP
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to