django rest framework - 序列化器期待pk获取字符串

时间:2014-09-16 16:59:06

标签: django django-rest-framework

我正在发送XML for post,它包含以下内容 -

<owner_id>Operations                    </owner_id>

我的序列化程序就是这样 -

class VariablePUTSerializer(serializers.ModelSerializer):

    class Meta:
        model = Varmst
        resource_name = 'varmst'
        fields = ('varmst_id', 'varmst_type', 'varmst_name', 'varmst_value', 'varmst_desc',
                'varmst_public', 'owner_id', 'varmst_lstchgtm', 'varmst_publish', 'varmst_readonly',
                'varmst_calc', 'varmst_starttype', 'varmst_startdt', 'varmst_startcal',
                'varmst_offsets', 'varmst_lstval')

    def transform_varmst_id(self, obj, value):
        maxid = Varmst.objects.latest('varmst_id').varmst_id
        if Varmst.objects.filter(varmst_name=obj.varmst_name).exists():
            obj.varmst_id = Varmst.objects.filter(varmst_name=obj.varmst_name).values_list('varmst_id')[0]
            return obj.varmst_id
        else:
            obj.varmst_id = maxid + 1
            return obj.varmst_id

    def transform_owner_id(self, obj, value):
        obj.owner_id = Owner.objects.filter(owner_name = obj.owner_id).values_list('owner_id')[0]
        return obj.owner_id

所以我正在做的是用关联的PK替换“操作”,以便用POST加载。

问题是,在尝试发帖时,我得到以下回复 -

[
    {
        "owner_id": [
            "Incorrect type.  Expected pk value, received str."
        ]
    }
]

0 个答案:

没有答案