在PUT期间,tastypie不会删除外键引用 [英] tastypie won't remove foreignkey reference during PUT

查看:136
本文介绍了在PUT期间,tastypie不会删除外键引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我做一个将外键字段设置为空的PUT时,我有一个Tastypie的问题不会保存对我的对象的更改。



这是我的ModelResource:

  class FolderResource(ModelResource):
parent = fields.ForeignKey('self','parent',full = True,default = None,blank = True,null = True)
project = fields.ForeignKey(ProjectResource,'project',full = False)
class Meta:
queryset = Folder.objects.all()
authentication = Authentication()
authorization = Authorization()
resource_name ='folder'
include_absolute_url = True
always_return_data = True
filters = {
slug:('exact','startswith',),
name
project:ALL_WITH_RELATIONS,
parent:ALL_WITH_RELATIONS,
id:('exact')
}
/ pre>

我有一个现有的文件夹对象,其中包含以下数据:

  {
absolute_url:/ projects / 1 / file s / 5 /,
created_date:2012年2月13日,
id:5,
modified_date:2012年2月15日,
modified_file:null,
name:testfolder2,
parent:{
absolute_url:/ projects / 1 / files / 1 /,
created_date:2012年2月4日,
id :1,
modified_date:2012年2月15日,
modified_file:null,
名称:testfolder1,
parent:null,
project: / projects / api / v1 / project / 1 /,
removed_date:null,
resource_uri:/ projects / api / v1 / folder / 1 /,
slug:testfolder1
},
项目:/ projects / api / v1 / project / 1 /,
removed_date:null,
resource_uri:/ projects / api / v1 / 5 /,
slug:testfolder2
}

我会尝试将以下数据输入到'/ projects / api / v1 / folder / 5 /':

  {
parent :null
}

我不g回到任何错误,一切似乎很好,但没有得到保存到数据库。任何人都可以告诉我我做错了什么,或者为什么没有保存变更?

解决方案

如果你想做部分更新,那么您需要一个PATCH方法。


I'm having an issue with Tastypie not saving changes to my object when I do a PUT that causes a foreignkey field to be set to null.

Here's my ModelResource:

class FolderResource(ModelResource):
    parent = fields.ForeignKey('self','parent',full=True,default=None,blank=True,null=True)
    project = fields.ForeignKey(ProjectResource,'project',full=False)
    class Meta:
        queryset = Folder.objects.all()
        authentication = Authentication()
        authorization = Authorization()
        resource_name = 'folder'
        include_absolute_url = True
        always_return_data = True
        filtering = {
            "slug": ('exact', 'startswith',),
            "name": ALL,
            "project":ALL_WITH_RELATIONS,
            "parent":ALL_WITH_RELATIONS,
            "id":('exact')
            }

I have an existing folder object with the following data:

{
    absolute_url: "/projects/1/files/5/",
    created_date: "13 Feb 2012",
    id: "5",
    modified_date: "15 Feb 2012",
    modified_file: null,
    name: "testfolder2",
    parent: {
        absolute_url: "/projects/1/files/1/",
        created_date: "4 Feb 2012",
        id: "1",
        modified_date: "15 Feb 2012",
        modified_file: null,
        name: "testfolder1",
        parent: null,
        project: "/projects/api/v1/project/1/",
        removed_date: null,
        resource_uri: "/projects/api/v1/folder/1/",
        slug: "testfolder1"
    },
    project: "/projects/api/v1/project/1/",
    removed_date: null,
    resource_uri: "/projects/api/v1/folder/5/",
    slug: "testfolder2"
}

I will try to PUT the following data to '/projects/api/v1/folder/5/':

{
    parent: null
}

I don't get back any errors, everything seems fine but nothing gets saved to the database. Can anyone tell me what I'm doing wrong or why the change isn't being saved?

解决方案

If you want to do a partial update, then you need a PATCH method.

这篇关于在PUT期间,tastypie不会删除外键引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆