try this
questions = self.context['request'].data.get("questions",None)

If questions is not in request.data it will be set to None. It will work
with your not condition as well.

On Mon, Apr 8, 2019 at 6:35 PM zohaib younis <zabi.jarra...@gmail.com>
wrote:

> arbaz thanks for your guidance i have resolved that issue now when i try
> to create a test it is giving me an error
> for question in data["questions"]:
> keyError :'questions'
> can you help me with that problem thanks
>
> On Mon, Apr 8, 2019 at 3:00 PM Arbaz Hundekar <
> arbaz.hunde...@avetoconsulting.com> wrote:
>
>> while instantiating your serializer do like this ser =
>> Serializer(context={"request": request})
>>
>> On Mon, Apr 8, 2019 at 1:57 PM zohaib younis <zabi.jarra...@gmail.com>
>> wrote:
>>
>>> def validate(self, data, *args, **kwargs):
>>> questions = self.context['request'].data.get("questions")
>>> if not questions:
>>> raise serializers.ValidationError("questions are required")
>>> if self.context["request"].method == "POST":
>>> self.questions = QuestionSerializer(data=questions, many=True)
>>> self.questions.is_valid(raise_exception=True)
>>> elif self.context["request"].method == "PUT":
>>> self.questions = questions
>>> self.new_questions = self.context["request"].data.get(
>>> "new_questions")
>>> if self.new_questions:
>>> self.new_questions = QuestionSerializer(
>>> data=self.new_questions, many=True)
>>> self.new_questions.is_valid(raise_exception=True)
>>> return data
>>>
>>> def create(self, data):
>>> evaluation_test = EvaluationTest()
>>> evaluation_test.category = data['category']
>>> evaluation_test.admin = data['admin']
>>> evaluation_test.title = data['title']
>>> evaluation_test.type = data['type']
>>> evaluation_test.save()
>>>
>>> for question in data['questions']:
>>> question.evaluationtest = evaluation_test
>>> question.save()
>>> return evaluation_test
>>>
>>> def update(self, instance, validated_data):
>>> instance.title = validated_data.get["title", instance.title]
>>> instance.type = validated_data.get["type", instance.type]
>>> instance.category = validated_data.get["category", instance.category]
>>> instance.admin = validated_data.get["admin", instance.admin]
>>>
>>> for question in self.questions:
>>> q = QuestionSerializer(instance=question["id"], data=question)
>>> q.is_valid(raise_exception=True)
>>> q.save()
>>>
>>> if self.new_questions:
>>> new_questions = self.new_questions.save()
>>> for question in new_questions:
>>> question.save()
>>> return instance
>>>
>>> --
>>> 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.
>>>
>>
>>
>> --
>> Warm Regards,
>>
>> Arbaz
>>
>> --
>> 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.
>


-- 
Warm Regards,

Arbaz

-- 
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