2013-10-31 31 views
0

我越來越努力拯救運動,我認爲這個問題是在哪裏bundle.obj.participant_reward(_SingleVoucherReward())我嘗試創建並保存新的資源之前鏈接它時,下面的錯誤。SingleVoucherReward」對象不是可調用的」

錯誤信息:SingleVoucherReward」對象不是可調用

注:對象SingleVoucherReward保存到數據庫成功的錯誤是連接並將其保存到Campaign

def hydrate(self, bundle, request=None): 
    """ 
    Tastypie uses a 'hydrate' cycle to take serializated data from the client 
    and turn it into something the data model can use. 
    """ 
    if bundle.data.get('SingleVouc'): 
     _SingleVoucherReward = SingleVoucherReward(
      description = "test", 
      subsequent_purchases = 1, 
      auto_reward = 1 

     ) 
     _SingleVoucherReward.save() 

     bundle.obj.participant_reward(_SingleVoucherReward()) 

    return bundle 

型號:

class Campaign(models.Model): 
    name = models.CharField(max_length=60, help_text="Give your campaign a name i.e Xmas Offer") 
    participant_reward_content_type = models.ForeignKey(ContentType, 
                 editable=False, 
                 related_name='%(app_label)s_%(class)s_as_participant', 
                 ) 
    participant_reward_object_id = models.PositiveIntegerField() 
    participant_reward = generic.GenericForeignKey('participant_reward_content_type', 'participant_reward_object_id') 

回答

4

您有一個名爲SingleVoucherReward模型,然後初始化的SingleVoucherReward命名_SingleVoucherReward的實例,但你的模型沒有定義__call__方法,使您得到這個not callable錯誤。

bundle.obj.participant_reward(_SingleVoucherReward()) 

它應該是:

bundle.obj.participant_reward(_SingleVoucherReward) 

順便說一句,命名single_voucher_reward是用來指明它是一個實例更加顯式。

+0

謝謝你,這是非常有益的。 – GrantU