django - Django REST Framework 序列化程序 PrimaryKeyRelatedField() 不在 GET 响应中添加对象
问题描述
我正在使用Django 2.x
和`Django REST Framework。
我有models.py,内容为
class ModeOfPayment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField()
class AmountGiven(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
contact = models.ForeignKey(Contact, on_delete=models.PROTECT)
amount = models.FloatField()
mode_of_payment = models.ForeignKey(
ModeOfPayment,
on_delete=models.PROTECT,
blank=True,
default=None,
null=True
)
和serializers.py
class AmountGivenSerializer(serializers.ModelSerializer):
mode_of_payment = serializers.PrimaryKeyRelatedField(queryset=ModeOfPayment.objects.all())
class Meta:
model = AmountGiven
depth = 1
fields = (
'id', 'contact', 'amount', 'mode_of_payment',
)
def update(self, instance, validated_data):
mode_of_payment = validated_data.pop('mode_of_payment')
instance.mode_of_payment_id = mode_of_payment.id
return instance
这很好用,因为我可以更新mode_of_payment
字段。但是在调用时响应amount_given
不包含mode_of_payment
对象的参数。
反应就像
{
"id": "326218dc-66ab-4c01-95dc-ce85f226012d",
"contact": {
"id": "b1b87766-86c5-4029-aa7f-887f436d6a6e",
"first_name": "Prince",
"last_name": "Raj",
"user": 3
},
"amount": 3000,
"mode_of_payment": "0cd51796-a423-4b75-a0b5-80c03f7b1e65",
}
在删除线的同时
mode_of_payment = serializers.PrimaryKeyRelatedField(queryset=ModeOfPayment.objects.all())
确实添加了mode_of_payment
带有响应的参数,但这一次不会更新mode_of_payment
.amount_given
为什么mode_of_payment
即使depth
设置为 1 也不包含数据。
解决方案
您可以ModeOfPaymentSerializer
在 AmountGivenSerializer 的to_representation()
方法中创建和使用它:
class ModeOfPaymentSerializer(serializers.ModelSerializer):
class Meta:
model = ModeOfPayment
fields = (
'id', 'title',
)
class AmountGivenSerializer(serializers.ModelSerializer):
mode_of_payment = serializers.PrimaryKeyRelatedField(queryset=ModeOfPayment.objects.all())
class Meta:
model = AmountGiven
fields = (
'id', 'contact', 'amount', 'mode_of_payment',
)
def update(self, instance, validated_data):
mode_of_payment = validated_data.pop('mode_of_payment')
instance.mode_of_payment_id = mode_of_payment.id
return instance
def to_representation(self, value):
data = super().to_representation(value)
mode_serializer = ModeOfPaymentSerializer(value.mode_of_payment)
data['mode_of_payment'] = mode_serializer.data
return data
推荐阅读
- javascript - 使我的@RenderBody 子页面符合浏览器调整大小
- django - Django-Oscar - 分叉嵌套应用程序
- c++ - c++项目链接静态和共享库
- javascript - 上传到 AWS s3 错误:“NetworkingError:网络故障”
- javascript - 如何判断导航栏是否折叠?
- image - Snowflake 中的什么数据类型适用于图像数据
- mysql - 过滤 ipv4 范围时的 sql 性能
- mysql - mysql。如何组合或匹配两个表中的特定行
- python - 与列表理解相比,是否有用于连接此 2d 列表的矢量化方法?
- php - 用户可以在浏览器中更改会话变量吗