2012-09-11 79 views
0

我已將以下「付款」字段添加爲與CheckResource的關係。django tastypie在創建時使用反向關係

class CheckResource(ModelResource): 
    user = fields.ForeignKey(UserResource, 'user', full=True) 
    venue = fields.ForeignKey(VenueResource, 'venue', full=False) 
    payments = fields.ToManyField('payments.api.PaymentResource', 
     attribute=lambda bundle: Payment.objects.filter(check=bundle.obj), 
     related_name=check, 
     full=True, 
     null=True, 
     blank=True 
    ) 

一切名單上的偉大工程/搞定了,但是當我嘗試創建,我得到以下錯誤:

{"error_message": "getattr(): attribute name must be string", "traceback": "Traceback (most recent call last):\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 192, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 397, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 427, in dispatch\n response = method(request, **kwargs)\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1165, in post_list\n updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs))\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1784, in obj_create\n self.save_m2m(m2m_bundle)\n\n File \"/Library/Python/2.7/site-packages/tastypie/resources.py\", line 1942, in save_m2m\n related_mngr = getattr(bundle.obj, field_object.attribute)\n\nTypeError: getattr(): attribute name must be string\n"} 

任何線索?

謝謝! 馬克

更新:我PaymentResource看起來是這樣的:

class PaymentResource(ModelResource): 
    from checks.api import CheckResource 
    venue = fields.ForeignKey(VenueResource, 'venue', full=False) 
    check = fields.ToOneField(CheckResource, 'check', full=False) 
    user = fields.ForeignKey(UserResource, 'user', full=False) 

回答

2

你缺少什麼是你的ToManyField定義related_name

Source in the documentation

+0

謝謝,我發現在你發佈的鏈接我的答案。我還必須將屬性值更改爲'payment_set'。 –