Django Rest 框架:关联 2 个表
Django Rest framework: Relate 2 tables
我是 python 和 Django 的新手。我有一个关于 2 个表的问题
参与者模型
lass Participant(models.Model):
id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
first_name = CharField(max_length=255)
last_name = CharField(max_length=255)
current_employing_country = CharField(max_length=255)
current_payroll_country = CharField(max_length=255)
# locations = ManyToManyField(Location)
位置模型
class Location(models.Model):
participant_id = ForeignKey(Participant, on_delete=CASCADE,related_name="locations")
start_date = DateField()
end_date = DateField()
country = CharField(max_length=255)
region = CharField(max_length=255)
tax_policy = CharField(max_length=255, null=True)
序列化器
class ParticipantSerializer(serializers.ModelSerializer):
class Meta:
model = Participant
fields = "__all__"
class LoactionSerializer(serializers.ModelSerializer):
# participant_id = serializers.PrimaryKeyRelatedField(queryset=Participant.objects.all())
class Meta:
model = Location
tax_policy = serializers.CharField(allow_blank=True, allow_null=True)
fields = "__all__"
尝试让所有具有 属性 个位置的参与者包含与参与者 ID 相关的所有位置的数组,例如 Participant.objects.all()。prefetch_related( “位置”)
示例-
[
{
"id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd",
"first_name": "test",
"last_name": "test",
"nationality": "test",
"current_employing_country": "israel",
"current_payroll_country": "dsadsa",
"locations": [ {
"id": 1,
"start_date": "2021-10-10",
"end_date": "2021-11-11",
"country": "isreal",
"region": "iseali",
"tax_policy": "N/A",
"participant_id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd"
},
{
"id": 2,
"start_date": "2021-10-10",
"end_date": "2021-11-11",
"country": "adsdsa",
"region": "iseali",
"tax_policy": "N/A",
"participant_id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd"
}]
}
]
就是下面这样。
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = "__all__"
class ParticipantSerializer(serializers.ModelSerializer):
locations = LocationSerializer(many=True)
class Meta:
model = Participant
fields = (
"id",
"first_name",
"last_name",
"locations",
)
我是 python 和 Django 的新手。我有一个关于 2 个表的问题
参与者模型
lass Participant(models.Model):
id = UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
first_name = CharField(max_length=255)
last_name = CharField(max_length=255)
current_employing_country = CharField(max_length=255)
current_payroll_country = CharField(max_length=255)
# locations = ManyToManyField(Location)
位置模型
class Location(models.Model):
participant_id = ForeignKey(Participant, on_delete=CASCADE,related_name="locations")
start_date = DateField()
end_date = DateField()
country = CharField(max_length=255)
region = CharField(max_length=255)
tax_policy = CharField(max_length=255, null=True)
序列化器
class ParticipantSerializer(serializers.ModelSerializer):
class Meta:
model = Participant
fields = "__all__"
class LoactionSerializer(serializers.ModelSerializer):
# participant_id = serializers.PrimaryKeyRelatedField(queryset=Participant.objects.all())
class Meta:
model = Location
tax_policy = serializers.CharField(allow_blank=True, allow_null=True)
fields = "__all__"
尝试让所有具有 属性 个位置的参与者包含与参与者 ID 相关的所有位置的数组,例如 Participant.objects.all()。prefetch_related( “位置”)
示例-
[
{
"id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd",
"first_name": "test",
"last_name": "test",
"nationality": "test",
"current_employing_country": "israel",
"current_payroll_country": "dsadsa",
"locations": [ {
"id": 1,
"start_date": "2021-10-10",
"end_date": "2021-11-11",
"country": "isreal",
"region": "iseali",
"tax_policy": "N/A",
"participant_id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd"
},
{
"id": 2,
"start_date": "2021-10-10",
"end_date": "2021-11-11",
"country": "adsdsa",
"region": "iseali",
"tax_policy": "N/A",
"participant_id": "a9a6c412-91c2-414a-97dc-8f8a4c6bdfcd"
}]
}
]
就是下面这样。
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = "__all__"
class ParticipantSerializer(serializers.ModelSerializer):
locations = LocationSerializer(many=True)
class Meta:
model = Participant
fields = (
"id",
"first_name",
"last_name",
"locations",
)