1. models.py
class Cluster(models.Model):
cluster_name = models.CharField(max_length=32, blank=True)
class Node(models.Model):
cluster_id = models.ForeignKey(Cluster, related_name='nodes', on_delete=models.PROTECT)
device_id = models.OneToOneField(Device, on_delete=models.PROTECT)
2.serializers.py
class NodeSerializer(serializers.ModelSerializer):
class Meta:
model = Node
fields = (
'id',
'cluster_id',
'device_id',
)
class ClusterSerializer(serializers.ModelSerializer):
# have to write "many=True"
nodes = NodeSerializer(many=True, read_only=True)
class Meta:
model = Cluster
fields = (
'id',
'cluster_name',
'nodes',
)