How Parent object will create in Django Restframework nested serializers? I want to show all children associated to the parent but the problem is that when I try to create Parent it asks children list and as per the rule first parent will born
models
class Parent(models.Model)
name = models.CharField(max_length=30)
class Child(models.Model)
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
Serializers
class ChildSerializer(ModelSerializer):
class Meta:
model = Child
fields = ('name')
class ParentSerializer(ModelSerializer):
children = ChildSerializer(many=True)
class Meta:
model = Parent
fields = ('name','children')
views.py
class ParentViewSet(ModelViewSet):
serializer_class = ParentSerializer
queryset = Parent.objects.all()
Response:
{
"children": [
"This field is required."
]
}