Django Tastypie没有使用ManyToManyField更新资源
为什么我的资源没有使用此PUT请求更新ManyToManyField?Django Tastypie没有使用ManyToManyField更新资源
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给出了这样的对话:
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/"}
这是非常类似于django tastypie manytomany field POST json error,但我没有在我的ManyToMany关系上使用通过属性。
问题resource_uri竟然是验证方法。使用FormValidation意味着uri like/api/v1/organizations/1 /不会验证为Django ORM的ForeignKey。使用自定义验证来修复问题。
许多Bothans为了给我们带来这些信息而死亡。
看起来你在OrganizationResource
设置DeviceResource
都ManyToManyField
和ForiegnKey
是full=True
。
所以当做一个PUT时,Tastypie需要给它一个完整的对象,或者至少有一个带有resource_uri的“空白”对象。
尝试发送的对象本身具有所指定的,而不是仅仅的URI即: {"resource_uri" : "/api/v1/organizations/1/"}
而不是"/api/v1/organizations/1/"
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/
感谢您的建议。当我尝试使用resource_uri键时,出现错误。堆栈跟踪的有趣部分是:{“error_message”:“int()参数必须是一个字符串或一个数字,而不是'dict'”,...我也删除了完整的= True部分,没有喜悦 - 它给了我与问题中所述相同的问题。 – Erik 2012-07-12 01:13:31
如果我同时执行这两个操作(使用resource_uri键并删除full = True),它也会导致错误。 – Erik 2012-07-12 01:15:09
这是Tastypie与full = True的限制。看到这个:https://github.com/toastdriven/django-tastypie/pull/382和最初的307问题。它正在解决0.9.12,但在此之前,你可以使用hydrate_favorites方法做你自己的事情。使用这个灵感:http://*.com/questions/11276099/django-tastypie-manytomany-saving-error – astevanovic 2012-07-12 01:26:53
你能告诉我们一个你使用什么样的验证的例子吗? – Amyth 2013-11-16 20:11:19