0

尝试保存 Campaign 时出现以下错误我认为问题出 bundle.obj.participant_reward(_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

1 回答 1

4

你有一个名为的模型SingleVoucherReward,然后初始化了一个SingleVoucherReward名为的实例_SingleVoucherReward,但是你的模型没有定义__call__方法,所以你得到这个not callable错误。

bundle.obj.participant_reward(_SingleVoucherReward())

它应该是:

bundle.obj.participant_reward(_SingleVoucherReward)

顺便说一句,命名single_voucher_reward更明确地表明它是一个实例。

于 2013-10-31T14:16:24.587 回答