Creating multiple django objects with the same foreign key object in one API call

Viewed 136

Lets say I've two models A and B where A has a ManyToOne Relationship with B modelled as shown below:

class A(models.Model):
    active = models.BooleanField(default=True)
    name = models.CharField(max_length=200, default='')

class B(models.Model):
    rank = models.IntegerField()
    name = models.CharField(max_length=200)
    a = models.ForeignKey(A, on_delete=models.CASCADE)

What I need is to be able to call an API to create an object of A and all the objects of B associated with A in one go. How should I set up the serializers for A and B so I can nest the creation of say, 4 B objects within the creation of one A object?

0 Answers
Related