What needs to happen?? What is happening right now? Is there any other code one might need to see?
Without above info none can help you. Regards, Chetan Ganji +91-900-483-4183 ganji.che...@gmail.com http://ryucoder.in On Thu, Mar 28, 2019 at 4:55 PM zohaib younis <zabi.jarra...@gmail.com> wrote: > class EvaluationTestSerializer(serializers.ModelSerializer): > questions = serializers.SerializerMethodField() > admin = StringSerializer(many=False) > category = StringSerializer(many=False) > class Meta: > model = EvaluationTest > fields='__all__' > > def get_questions(self,obj): > questions = QuestionSerializer(obj.questions.all(), many=True).data > return questions > > def create(self,request): > data = request.data > print(data) > evaluationtest = EvaluationTest() > admin= User.objects.get(username=data['admin']) > cat=Category.objects.get(id=data['category']) > print(cat) > evaluationtest.category=cat > evaluationtest.admin = admin > evaluationtest.title = data['title'] > evaluationtest.type = data['type'] > evaluationtest.save() > order = 1 > for q in data['questions']: > newQ = Question() > newQ.question=q['title'] > newQ.order = order > newQ.save() > > for c in q['choices']: > newC = Choice() > newC.title = c > newC.save() > newQ.choices.add(newC) > newQ.answer = Choice.objects.get(title=q['answer']) > newQ.evaluationtest = evaluationtest > newQ.save() > order += 1 > return evaluationtest > > # def update(self,instance,validated_data): > # # data = request.data > # # print(data) > # # evaluationtest = EvaluationTest() > # # admin= User.objects.get('username') > # # cat=Category.objects.get('id') > # # print(cat) > # # instance.category=cat > # instance.category = validated_data.get('id',instance.category) > # instance.title = validated_data.get('title',instance.title) > # instance.type = validated_data.get('type',instance.type) > # instance.save() > > # instance = EvaluationTest() > # # keep_questions=[] > # # existing_id=[q.id for q in instance.questions] > # order = 1 > # for q in instance['questions']: > # newQ = Question() > # newQ.question=q['title'] > # newQ.order = order > # newQ.save() > > # for c in q['choices']: > # newC = Choice() > # newC.title = c > # newC.save() > # newQ.choices.add(newC) > # newQ.answer = Choice.objects.get(title=q['answer']) > # newQ.instance = instance > # newQ.save() > # order += 1 > # 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. > -- 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.