我正在使用Django休息框架 这是我的Serializer:
class IncentiveSerializer(serializers.ModelSerializer):
tags=TagSerializer(many=True, read_only=False)
class Meta:
model = Incentive
fields = ('schemeName', 'schemeID','text','typeID','typeName','status','ordinal','tags','modeID',
'groupIncentive','condition')
def create(self, validated_data):
tags_data = validated_data.pop('tags',[])
incentive = super(IncentiveSerializer, self).create(validated_data)
for tag in tags_data:
if tag is not None:
tags=Tag.objects.create(incentiveID=incentive, **tag)
# Ignores tags without a tagId
tags_ids = [tag["tagID"] for tag in tags_data if "tagID" in tag]
if tags_ids:
tags = Tag.objects.filter(tagId__in=tags_ids)
incentive.tags.add(*tags)
else:
incentive.tags.add(*tags)
return incentive
我一直在发布POST时,tags_data为空[{}] 这就是我试图发布的内容:
{
"schemeName": "ghjgkj",
"schemeID": 2314,
"text": "asgas",
"typeID": 123,
"typeName": "asga",
"status": false,
"ordinal": null,
"tags": [{"tagID":1232,"tagName":"DO"}],
"modeID": 123,
"groupIncentive": false,
"condition": "asdfga"
}
我试图将一个文件打印到tags_data中,并且它总是为空 我做错了什么? (我用json.dumps打印成文件) 如何从帖子中获取标签参数?我的帖子有问题吗?
修改:
这是tagserializer
:
class TagSerializer(serializers.Serializer):
class Meta:
model = Tag
fields = ('tagID', 'tagName')