序列化程序.py
class Car(serializers.ModelSerializer):
geography=Geography(read_only=True)
class Meta:
model = Car
fields = ['car_id', 'geography']
模型.py
class Car(models.Model):
car_id = models.IntegerField(null=False)
geography = models.ForeignKey('Geography' ,on_delete=models.CASCADE)
我想要的对象:
[
{
"car_id": 1,
"geography":[ {
"key": 1,
"name": "India"},
{"key": 1,
"name": "China"
}
]
}
]
我得到的对象:
[
{
"car_id": 1,
"geography": {
"key": 1,
"name": "India"}
}
]
我正在使用的数据具有 car_id 和 key 属性的重复值。我还有另一个困惑是应该是外键还是多对多字段。任何帮助,将不胜感激