当我尝试更新我的资源时,我不断收到此错误。
我要更新的资源称为 Message。它有一个外键记账:
class AccountResource(ModelResource):
class Meta:
queryset = Account.objects.filter()
resource_name = 'account'
'''
set to put because for some weird reason I can't edit
the other resources with patch if put is not allowed.
'''
allowed_methods = ['put']
fields = ['id']
def dehydrate(self, bundle):
bundle.data['firstname'] = bundle.obj.account.first_name
bundle.data['lastname'] = bundle.obj.account.last_name
return bundle
class MessageResource(ModelResource):
account = fields.ForeignKey(AccountResource, 'account', full=True)
class Meta:
queryset = AccountMessage.objects.filter(isActive=True)
resource_name = 'message'
allowed = ['get', 'put', 'patch', 'post']
authentication = MessageAuthentication()
authorization = MessageAuthorization()
filtering = {
'account' : ALL_WITH_RELATIONS
}
现在,当我尝试使用 PATCH 更新我的消息时,我收到此错误:
传入的数据:{"text":"blah!"}
The 'account' field has was given data that was not a URI, not a dictionary-alike and does not have a 'pk' attribute: <Bundle for obj: '<2> [nikunj]' and with data: '{'lastname': u'', 'id': u'2', 'firstname': u'', 'resource_uri': '/api/v1/account/2/'}'>.
不好的解决方案::
传入数据:{"text":"blah!", "account":{"pk":2}}
我不想传入帐户。我只想编辑文本而不是别的。为什么还需要转帐呢?
我尝试使用:
def obj_update(self, bundle, request=None, **kwargs):
return super(ChartResource, self).obj_update(bundle, request, account=Account.objects.get(account=request.user))
但它不起作用! 帮助!