有什么方法可以在 Postman 中的 model.py 类型字符串中显示 status_choices 吗?我的模型.pyclass Transaction(models.Model): user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE) address = models.TextField() status_choices = ( (1, 'in processing'), (2, 'in delivery'), (3, 'done'), (4, 'cancel'), ) status = models.CharField(max_length=1, choices=status_choices) total = models.BigIntegerField() phone_number = models.CharField(max_length=10) variants = models.ManyToManyField(Variant, through='TransactionVariant') created_at = models.DateTimeField(auto_now_add=True, blank=True) updated_at = models.DateTimeField(auto_now_add=True, blank=True) deleted_at = models.DateTimeField(null=True, blank=True) class Meta: db_table = "transaction"序列化程序.pyclass TransactionSerializer(serializers.Serializer): # status = StatusSerializer(many=False, read_only=True) user_id = serializers.IntegerField() transaction_id = serializers.IntegerField(source='id') product = serializers.CharField(source='name') status = serializers.CharField() address = serializers.CharField() total = serializers.IntegerField() phone_number = serializers.IntegerField() created_at = serializers.DateTimeField(format='%H:%M %d %b %Y')我对邮递员的回应: "user_id": 5, "transaction_id": 1, "product": "product1", "status": "3",----------> I want this field return what exactly in my model designer "address": "trung nu vuong 3", "total": 123, "phone_number": 1234567890, "created_at": "09:35 21 Jan 2019"我要回复回复{status: done}
1 回答
慕森王
TA贡献1777条经验 获得超3个赞
您需要使用 SerializerMethodField
status = serializers.SerializerMethodField("get_status_as_char")
def get_status_as_char(self, obj):
return obj.get_status_display()
添加回答
举报
0/150
提交
取消