Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django Tastypie未使用ManyToManyField更新资源_Python_Django_Tastypie_M2m - Fatal编程技术网

Python Django Tastypie未使用ManyToManyField更新资源

Python Django Tastypie未使用ManyToManyField更新资源,python,django,tastypie,m2m,Python,Django,Tastypie,M2m,为什么我的ManyToManyField资源没有更新这个PUT请求 curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"uuid":"blah","pass_token":"blah","favorites": ["/api/v1/organizations/1/"]}' http://localhost:8000/api/v1/devices/2/ 我得到的答复是: HTTP/1.0 400 BA

为什么我的ManyToManyField资源没有更新这个PUT请求

curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"uuid":"blah","pass_token":"blah","favorites": ["/api/v1/organizations/1/"]}' http://localhost:8000/api/v1/devices/2/
我得到的答复是:

HTTP/1.0 400 BAD REQUEST
Date: Wed, 11 Jul 2012 22:21:15 GMT
Server: WSGIServer/0.1 Python/2.7.2
Content-Type: application/json; charset=utf-8

{"favorites": ["\"/api/v1/organizations/1/\" is not a valid value for a primary key."]}
以下是我的资源:

class OrganizationResource(ModelResource):
    parent_org = fields.ForeignKey('self','parent_org',null=True, full=True,blank=True)

    class Meta:
        allowed_methods = ['get',]
        authentication = APIAuthentication()
        fields = ['name','org_type','parent_org']
        filtering = {
            'name': ALL,
            'org_type': ALL,
            'parent_org': ALL_WITH_RELATIONS,
        }
        ordering = ['name',]
        queryset = Organization.objects.all()
        resource_name = 'organizations'

class DeviceResource(ModelResource):
    favorites = fields.ManyToManyField(OrganizationResource,'favorites',null=True,full=True)

    class Meta:
        allowed_methods = ['get','patch','post','put',]
        authentication = APIAuthentication()
        authorization = APIAuthorization()
        fields = ['uuid',]
        filtering = {
            'uuid': ALL,
        }
        queryset = Device.objects.all()
        resource_name = 'devices'
        validation = FormValidation(form_class=DeviceRegistrationForm)
在OrganizationResource上的get为此exchange提供:

curl --dump-header - -H "Content-Type: application/json" -X GET http://localhost:8000/api/v1/organizations/1/

HTTP/1.0 200 OK
Date: Wed, 11 Jul 2012 22:38:30 GMT
Server: WSGIServer/0.1 Python/2.7.2
Content-Type: application/json; charset=utf-8

{"name": "name", "org_type": "org_type", "parent_org": null, "resource_uri": "/api/v1/organizations/1/"}

这与非常相似,但我没有在我的多人关系上使用through属性。

看起来您在
DeviceResource
中将
ManyToManyField
OrganizationResource
中的
ForiegnKey
都设置为
full=True

因此,在执行PUT时,Tastypie需要一个完整的对象,或者至少是一个带有资源uri的“空白”对象

尝试使用指定的资源uri(而不仅仅是uri)发送对象本身,即:
{“资源uri”:/api/v1/organizations/1/“}
而不是
“/api/v1/organizations/1/”


看起来您将
devicesource
中的
ManyToManyField
OrganizationResource
中的
ForiegnKey
都设置为
full=True

因此,在执行PUT时,Tastypie需要一个完整的对象,或者至少是一个带有资源uri的“空白”对象

尝试使用指定的资源uri(而不仅仅是uri)发送对象本身,即:
{“资源uri”:/api/v1/organizations/1/“}
而不是
“/api/v1/organizations/1/”


问题是验证方法。使用FormValidation意味着像/api/v1/organizations/1/这样的uri不会作为Django ORM的外键进行验证。改为使用自定义验证解决了该问题


许多博萨人为了给我们带来这些信息而牺牲了。

问题在于验证方法。使用FormValidation意味着像/api/v1/organizations/1/这样的uri不会作为Django ORM的外键进行验证。改为使用自定义验证解决了该问题


许多博萨人为了给我们带来这些信息而牺牲。

谢谢你的建议。当我尝试使用resource_uri键时,我得到一个错误。堆栈跟踪中有趣的部分是:{“error_message”:“int()参数必须是字符串或数字,而不是'dict'”,我也毫无喜悦地删除了full=True部分——它给了我与问题中所述相同的问题。如果我同时做这两件事(使用resource_uri键并删除full=True),也会导致错误同时。这是使用full=True的Tastypie的一个限制。请参见:以及最初的307问题。它将在0.9.12中解决,但在此之前,您可以使用Hyde_favorites方法做您自己的事情。使用此方法获得灵感:好的链接。如果我删除full=True,为什么我的方法不起作用?如果不通过Tastypie co进行跟踪,则不确定使用调试器删除resource.py。这可能是因为您在组织上具有递归关系?我的建议是将您的示例简化为最基本的示例,在该示例中PUT可以工作并从中构建。我假设您也从父组织中删除了full?感谢您的建议。当我尝试使用resource\u uri键时,我遇到了一个错误。堆栈跟踪的有趣部分是:{“error_message”:“int()参数必须是字符串或数字,而不是'dict'”。我也毫无喜悦地删除了full=True部分——它给了我与问题中所述相同的问题。如果我同时执行这两项操作(使用resource_uri键和delete full=True),也会导致错误同时。这是使用full=True的Tastypie的一个限制。请参见:以及最初的307问题。它将在0.9.12中解决,但在此之前,您可以使用Hyde_favorites方法做您自己的事情。使用此方法获得灵感:好的链接。如果我删除full=True,为什么我的方法不起作用?如果不通过Tastypie co进行跟踪,则不确定使用调试器删除resource.py。这可能是因为您在组织上有递归关系?我的建议是将您的示例简化为最基本的示例,在该示例中PUT可以工作并从中构建。我假设您也从父组织中删除了full?您能给我们展示一个您使用的验证类型的示例吗?您能给我们展示一下吗您使用哪种验证的示例?
curl --dump-header - -H "Content-Type: application/json" -X PUT --data '{"uuid":"blah","pass_token":"blah","favorites": [{"resource_uri" : "/api/v1/organizations/1/"}]}' http://localhost:8000/api/v1/devices/2/