Make two records on the same table Department by foreingn key relation using nested serializer. ,this Department two records are creating at same time the People table created
class Department(BaseModel):
name = models.CharField(max_length=50)
age = models.PositiveIntegerField()
class People(BaseModel):
teacher = models.ForeignKey(Department, on_delete=models.PROTECT,related_name='teacher')
student = models.ForeignKey(Department, on_delete=models.PROTECT, related_name='student')
available_seats = models.PositiveIntegerField(default=0)
class DepartmentSerializer(serializers.ModelSerializer):
class Meta:
model = Department
fields = '__all__'
class PeopleSerializer(WritableNestedModelSerializer):
teacher = DepartmentSerializer(many=True, write_only=True)
student = DepartmentSerializer(many=True, write_only=True)
available_seats = models.PositiveIntegerField()
class Meta:
model = People
fields = '__all__'
Here I need to accept this input and create records in People and Department table
Input
{
"available_seats":100,
"teacher": [{
"name": John,
"age" : 37
}],
"student": [{
"name": John,
"age" : 37
}]
}
But i am getting this error "Direct assignment to the reverse side of a related set is prohibited. Use student.set() instead."
thanks in advance