Search code examples
pythondjangodjango-modelspostmandjango-serializer

Trying to add data using postman, when having nested serializer


i have two models students and marks, i've list of students i need to add marks using postman in a nested serializer class. How can i select already existing student?

Models.py

class Student(models.Model):
    name = models.CharField(max_length=50, null=False, blank=False)
    roll_no = models.CharField(max_length=5, null=False, blank=False)
    mobile_no = PhoneNumberField(null=False, blank=False, unique=True)
    mail_id = models.EmailField(null=False, blank=False)

    def __str__(self):
        return self.name

class Marks(models.Model):
    student = models.ForeignKey(Student, on_delete=models.CASCADE)
    maths = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)])
    science = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)])
    social = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)])

    def __str__(self):
        return str(self.student)

Serializers.py

class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = '__all__'

class MarksSerializer(serializers.ModelSerializer):

    student = StudentSerializer()
    class Meta:
        model = Marks
        fields = '__all__'

if i give dictionary i'm creating new student, i dont want to create on i want to select existing student, How can i do that?

how to send data through postman, when you have a nested serializer class?


Solution

  • If you are looking to select an existing student when adding marks and not create a new student, you might consider removing the StudentSerializer from MarksSerializer. You can use a PrimaryKeyRelatedField which allows you to represent the relationship using the primary key of the Student model. This way, you only need to send the primary key (or id) of the student when creating marks using Postman.

    Serializers.py

    from rest_framework import serializers from .models import Student, Marks

    class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = '__all__'
    

    class MarksSerializer(serializers.ModelSerializer):

    student = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all())
    class Meta:
        model = Marks
        fields = '__all__'
    

    Sending Data through Postman:

    To send data through Postman:

    Select the HTTP method as POST. Enter the URL of the API endpoint you are trying to hit. Go to the Body tab, select raw and JSON (application/json). Send the data in the following format:

    {
        "student": 1, 
        "maths": 95,
        "science": 89,
        "social": 92
    }
    

    Here, student is the ID of the existing student. The maths, science, and social fields are the marks to be added for that student.

    If you have set up everything correctly, this should work and it will not create a new student but select an existing one by ID.