我想将非平面结构序列化为一个扁平对象。 这是我收到的API调用的一个例子(遗憾的是我无法控制它):
{
"webhookEvent": "jira:issue_updated",
"user": {
"id": 2434,
"name": "Ben",
},
"issue": {
"id": "33062",
"key": "jira-project-key-111",
"fields": {
"summary": "The week ahead",
},
"changelog": {
"id": "219580",
"items": [{
"field": "status",
"fieldtype": "jira",
"from": "10127",
"fromString": "Submitted",
"to": "10128",
"toString": "Staged"
}]
},
"timestamp": 1423234723378
}
我想把它序列化为这样的模型:
class Issue(models.Model):
jira_id = models.IntegerField()
jira_id = models.CharField()
summary = models.CharField()
class Change(models.Model):
issue = models.ForeignKey(Issue)
timestamp = models.DataTimeField()
如您所见,模型Issue
的字段summary
与id
和key
位于同一对象上,与JSON数据不同。
My Serializer是下一个:
class ChangeSerializer(serializers.ModelSerializer):
"""Receives complex data from jira and converts into objects."""
issue = JiraIssueSerializer()
timestamp = TimestampField(source='created_at')
class Meta:
model = Change
fields = ('issue', 'timestamp')
def create(self, validated_data):
super(serializers.ModelSerializer, self).create(validated_data=validated_data)
jira_issue = JiraIssueSerializer(data=validated_data)
issue = Issue.objects.get(jira_issue)
self.created_at = datetime.utcnow()
change = Change(**validated_data)
return change
class JiraIssueSerializer(serializers.ModelSerializer):
"""Issue serializer."""
id = serializers.IntegerField(source='jira_id')
key = serializers.CharField(source='jira_key')
summary = serializers.CharField() ### I want field to work!
# fields = serializers.DictField(child=serializers.CharField())
class Meta:
model = Issue
fields = ('id', 'key',
'summary',
)
def to_internal_value(self, data):
# ret = super(serializers.ModelSerializer, self).to_internal_value(data)
ret = {}
# ret = super().to_internal_value(data)
ret['jira_id'] = data.get('id', None)
ret['jira_key'] = data.get('key', None)
jira_issue_fields_data = data.get('fields')
if jira_issue_fields_data or 1:
summary = jira_issue_fields_data.get('summary', None)
ret.update(summary=summary)
print('to_internal_value', ret)
return ret
def to_representation(self, instance):
ret = {}
ret = super().to_representation(instance)
fields = {}
fields['summary'] = instance.summary
ret.update(fields=fields)
print(ret)
return ret
我适用于JSON中issue
对象中的字段。
但是,我如何向JiraIssueSerializer添加一些字段,如summary
?它们不是issue
对象的直接字段,而是位于子结构fields
中。
我看到了这些方式:
再制作一个模型Fields
来保留它们,但这太荒谬了。我不需要它,我的API严格依赖于外来结构。
制作一些.to_internal_fields()
转换器。但在这种情况下,我必须手动验证并准备我Issue
中的所有字段并重复几次。此外,如果字段summary
未在Serializer中登记,则后者无法使用它或验证失败。
将DictField
添加到Serializer(在代码中注释)并从中获取数据。好吗?如何验证?同样,我有一个干净的代码结构。
接下来,我想解析并保存更改日志数据。
如何更好地处理此类结构?
谢谢!
答案 0 :(得分:10)
最后,在django-rest-framework的测试中发现了解决方案 https://github.com/tomchristie/django-rest-framework/blob/master/tests/test_serializer.py#L149
您可以轻松定义嵌套序列化程序,它们将充当容器并将数据提取到普通对象。像这样:
class NestedSerializer1(serializers.Serializer):
a = serializers.IntegerField()
b = serializers.IntegerField()
class NestedSerializer2(serializers.Serializer):
c = serializers.IntegerField()
d = serializers.IntegerField()
class TestSerializer(serializers.Serializer):
nested1 = NestedSerializer1(source='*')
nested2 = NestedSerializer2(source='*')
data = {
'nested1': {'a': 1, 'b': 2},
'nested2': {'c': 3, 'd': 4}
}
serializer = TestSerializer(data=self.data)
assert serializer.is_valid()
assert serializer.validated_data == {
'a': 1,
'b': 2,
'c': 3,
'd': 4
}
答案 1 :(得分:0)
我建议你创建自己的自定义序列化程序来接收数据。你可以这样做:
from rest_framework import serializers
class MySerializer(serializers.Serializer):
"""
Custom serializer
"""
id = serializers.IntegerField(read_only=True)
name = serializers.CharField()
def create(self, validated_data):
"""Create a new object"""
validated_data['custom_value'] = 0 # you can manipulate and restructure data here if you wish
return MyModel.objects.create(**validated_data)
然后,您可以根据需要在create()
功能中操作数据。您还可以创建嵌套的自定义序列化程序来解析此数据。
答案 2 :(得分:0)
文档是处理nesting in serialisation的重要部分。
基本上,您要创建一个具有如下嵌套值的单独类:
class UserSerializer(serializers.Serializer):
email = serializers.EmailField()
username = serializers.CharField(max_length=100)
class CommentSerializer(serializers.Serializer):
user = UserSerializer()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()