尝试保存广告事件
时收到错误消息'NoneType'对象不可调用。在 Django 中,这是否意味着一旦我保存了对象 SingleVoucherReward()
,我就无权访问它并且无法将其分配给我的 Campaign 对象?
single_voucher_reward = SingleVoucherReward()
single_voucher_reward.description = "test"
single_voucher_reward.save()
Campaign.participant_reward(single_voucher_reward)
Campaign.save()
型号:
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')
最佳答案
您正在尝试将事物分配给类,而不是类的实例。尝试:
campaign = Campaign()
campaign.participant_reward = single_voucher_reward
campaign.save()
或
campaign = Campaign(participant_reward = single_voucher_reward)
campaign.save()
关于python - 添加保存的 FK 时 NoneType 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709394/