尝试保存 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')