我正在尝试在Django中创建一个enum field,在GET请求时将返回枚举的文本表示,并在POST或PATCH请求时将文本表示转换为相应的整数,然后再保存。
transform_<field>()
方法可以很好地将整数枚举值转换为相应的字符串,但我无法找到一种更好的方法将字符串转换为除了黑客攻击之外的相应整数
validate_<field>()
方法
有更好的方法吗?请参阅下面的代码
模型文件
class Status(enum.Enum):
RUNNING = 0
COMPLETED = 1
labels = {
RUNNING: 'Running',
COMPLETED: 'Completed'
}
translation = {v: k for k, v in labels.iteritems()}
class Job(models.Model):
status = enum.EnumField(Status)
Serializer
class JobSeralizer(serializers.ModelSerailzer):
status = seralizers.CharField(max_length=32, default=Status.QUEUED)
def transform_status(self, obj, value):
return JobStatus.labels[value]
def validate_status(self, attrs, source):
"""Allow status to take numeric or character representation of status
"""
status = attrs[source]
if status in JobStatus.translation:
attrs[source] = JobStatus.translation[status]
elif status.isdigit():
attrs[source] = int(status)
else:
raise serializers.ValidationError("'%s' not a valid status" % status)
return attrs
答案 0 :(得分:5)
如OP所述,您可以使用drf v3.x中的custom fields轻松完成此操作。以下是用于转换值&lt; - &gt;的通用自定义字段的快速示例标签(例如枚举值&lt; - &gt;文字表示):
class KeyValueField(serializers.Field):
""" A field that takes a field's value as the key and returns
the associated value for serialization """
labels = {}
inverted_labels = {}
def __init__(self, labels, *args, **kwargs):
self.labels = labels
# Check to make sure the labels dict is reversible, otherwise
# deserialization may produce unpredictable results
inverted = {}
for k, v in labels.iteritems():
if v in inverted:
raise ValueError(
'The field is not deserializable with the given labels.'
' Please ensure that labels map 1:1 with values'
)
inverted[v] = k
self.inverted_labels = inverted
return super(KeyValueField, self).__init__(*args, **kwargs)
def to_representation(self, obj):
if type(obj) is list:
return [self.labels.get(o, None) for o in obj]
else:
return self.labels.get(obj, None)
def to_internal_value(self, data):
if type(data) is list:
return [self.inverted_labels.get(o, None) for o in data]
else:
return self.inverted_labels.get(data, None)
字段初始化看起来像这样:
class MySerializer(serializers.Serializer):
afield = KeyValueField(labels={0:'enum text 0', 1:'enum text 1'})