gpt4 book ai didi

python - 添加保存的 FK 时 NoneType 对象不可调用

转载 作者:行者123 更新时间:2023-12-01 05:33:15 25 4
gpt4 key购买 nike

尝试保存广告事件时收到错误消息'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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com