枚举 Python 如何响应

有什么方法可以在 Postman 中的 model.py 类型字符串中显示 status_choices 吗?


我的模型.py


class 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"

序列化程序.py


class 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

}


青春有我
浏览 186回答 1
1回答

慕森王

您需要使用 SerializerMethodFieldstatus = serializers.SerializerMethodField("get_status_as_char")def get_status_as_char(self, obj):   return  obj.get_status_display()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python