I am almost new to django and rest framework. I want to get data on a request from client side and save a model's object. In my view.py file I have a class that extends from APIView(for example named CreateUser(APIView). From the client side, I want to post a JSON file and get it in this view and save it in my models. How can I handle it?
My Student class is:
from django.contrib.auth.models import User from django.db import models class Student(models.Model): user = models.ForeignKey(User) grade = models.IntegerField() I have created a serializer for my Student class.
from rest_framework import serializers from users.models import Student class StudentSerializer(serializers.ModelSerializer): class Meta: model = Student fields = ('username', 'password', 'grade') def create(self, validated_data): user = validated_data.get('user') grade = validated_data.get('grade') student = Student.objects.create(self, user=user, grade=grade) return student I want to get a JSON object with username password and grade and save it in my models.