我正在尝试使用 PATCH 请求发布多个对象,但出现此错误:
"error_message": "'HttpRequest' object has no attribute 'user'"
我的资源模型如下:
class TrackerResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Tracker.objects.all()
resource_name = 'tracker'
allowed_methods = ['post','patch','put']
detail_allowed_methods = ['post','patch','put']
authentication = ApiKeyAuthentication()
authorization = Authorization()
always_return_data = True
fields = ['data','tracker_date','badges','module','completed']
def hydrate(self, bundle, request=None):
bundle.obj.user = bundle.request.user
bundle.obj.ip = bundle.request.META.get('REMOTE_ADDR','0.0.0.0')
bundle.obj.agent = bundle.request.META.get('HTTP_USER_AGENT','unknown')
return bundle
我提出的 curl 请求是(使用正确的 api_key):
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X PATCH --data '{"objects":[{"digest":"e5b24a362259b1408161829737f8ef3c","data":"{}","completed":1},{"digest":"e5b24a362259b1408161829737f8ef3c","data":"{}","completed":0}]}' "http://localhost/python/modules/api/v1/tracker/?username=alex&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx"
如果我只发布一个对象,它工作正常:
curl --dump-header - -H "Accept: application/json" -H "Content-Type: application/json" -X POST --data '{"digest":"e5b24a362259b1408161829737f8ef3c","data":"{}","completed":0}' "http://localhost/python/modules/api/v1/tracker/?username=alex&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx"
那么,当我发出 PATCH 请求时,能够访问当前用户对象的正确方法是什么?还是我需要做一些事情来使 PATCH 请求将当前请求发送到每个水合物方法?
非常感谢任何帮助。