Django post是空的

2024-09-25 12:27:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Django rest框架 这是我的序列化程序:

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

我在尝试发布标签时,所有的数据都是空的[{}] 这就是我想发布的内容:

{
    "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"
}

我试着把标签和数据打印到一个文件里,结果总是空的 我做错了什么?(我正在打印一个文件json.dumps文件) 如何从发布中获取标签参数?我的帖子有问题吗?你知道吗

编辑: 这是tagserializer

    class TagSerializer(serializers.Serializer):
        class Meta:
            model = Tag
            fields = ('tagID', 'tagName')

Tags: 文件inidsdataiftagcreatetags