问题
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?
回答1:
If you want to do a partial update, then you need a PATCH method.
来源:https://stackoverflow.com/questions/9307619/tastypie-wont-remove-foreignkey-reference-during-put