# models.py
class ModelTwo(models.Model):
number = models.CharField(max_length=12, unique=True)
class ModelOne(models.Model):
model_two = models.ForeignKey("ModelTwo", related_name="model_two", on_delete=model.CASCADE)
# serializers.py
class ModelOneSerializer(serializers.ModelSerializer):
model_two = serializers.PrimaryKeyRelatedField(queryset=ModelTwo.objects.all())
class Meta:
model = ModelOne
fields = ("id", "model_two")
class ModelOneSerializer(serializers.ModelSerializer):
model_two = ModelTwo(read_only=True, many=True)
class Meta:
model = ModelOne
fields = ("id", "model_two")