在Django Rest框架序列化器中是否有任何方法可以忽略选择字段中的大小写?

时间:2015-10-07 09:53:15

标签: python django django-rest-framework

<html>
<head>
    <title>Campaign progress dashboard</title>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script>
        var graph_data = {};
        var options = {};
        var material = {};
        var data = {};
        function drawDualX() {
            graph_data["clientA"] = [[
                {label: 'Campaign', type: 'string'},
                {label: 'Days passed', type: 'number'},
                {label: 'Budget spent', type: 'number'}
            ]];

            graph_data["clientA"].push([
                "clientA - YT",
                28.5714285714,
                1.29998611111
            ]);

            graph_data["clientA"].push([
                "clientA - search",
                43.8775510204,
                27.1253170732
            ]);

            data["clientA"] = google.visualization.arrayToDataTable(graph_data["clientA"]);
            options["clientA"] = {
                width: 800,
                legend: {
                    position: 'bottom'
                },
                height: graph_data["clientA"].length * 75 + 40,
                chart: {
                    title: "Campaign progress of clientA"
                },
                axes: {
                    x: {
                        0: {
                            side: 'top',
                            label: '% Progress',
                            viewWindow: {
                                min: 0,
                                max: 100
                            }
                        }
                    }
                },
                bars: 'horizontal'
            };

            material["clientA"] = new google.charts.Bar(document.getElementById("clientA"));
            material["clientA"].draw(data["clientA"], options["clientA"]);

            graph_data["clientB"] = [[
                {label: 'Campaign', type: 'string'},
                {label: 'Days passed', type: 'number'},
                {label: 'Budget spent', type: 'number'}
            ]];

            graph_data["clientB"].push([
                "GDN_gfx_09",
                120.689655172,
                71.3906666667
            ]);

            graph_data["clientB"].push([
                "Search_09",
                120.689655172,
                74.40475
            ]);

            graph_data["clientB"].push([
                "Search_ALL",
                75.7746478873,
                63.5448310388
            ]);

            graph_data["clientB"].push([
                "Search_ALL_H2",
                28.3333333333,
                17.2075238095
            ]);

            graph_data["clientB"].push([
                "GDN_gfx_ALL_H2",
                28.3333333333,
                17.4727352941
            ]);

            graph_data["clientB"].push([
                "GDN_gfx_ALL",
                75.7746478873,
                67.2122571128
            ]);

            data["clientB"] = google.visualization.arrayToDataTable(graph_data["clientB"]);
            options["clientB"] = {
                width: 800,
                legend: {
                    position: 'bottom'
                },
                height: graph_data["clientB"].length * 75 + 40,
                chart: {
                    title: "Campaign progress of clientB"
                },
                axes: {
                    x: {
                        0: {
                            side: 'top',
                            label: '% Progress',
                            viewWindow: {
                                min: 0,
                                max: 100
                            }
                        }
                    }
                },
                bars: 'horizontal'
            };

            material["clientB"] = new google.charts.Bar(document.getElementById("clientB"));
            material["clientB"].draw(data["clientB"], options["clientB"]);
        }

        google.setOnLoadCallback(drawDualX);
        google.load('visualization', '1.1', {packages: ['bar']});
    </script>
</head>
<body>
    <div id="clientA" style="margin:auto; width: 800px;"></div>
    <p></p>
    <div id="clientB" style="margin:auto; width: 800px;"></div>
</body>
</html>

所以我的输入可以是以下之一。 不,不,是的,是的

对于这些我需要为Capital 1增加2个条目吗?

class MySerializer(serializers.Serializer):
  contract = fields.ChoiceField(choices=(
    ('no', 'no'),
    ('yes', 'yes'),
  ))

或者我们有什么办法可以忽略这个案子?

1 个答案:

答案 0 :(得分:1)

http://www.django-rest-framework.org/api-guide/fields/#choicefield

如果您想将其留给用户,您可能需要考虑更多选项,例如“是”,“是”,“是”而不仅仅是“是”,是“

我更喜欢你将它们转换为低级并使用.lower()将其设置在字段上,这样你总是会得到小写字母作为输入