1

我在尝试保存时收到错误消息“NoneType”对象不可调用Campaign。在 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')
4

1 回答 1

2

您正在尝试将事物分配给类,而不是类的实例。尝试:

campaign = Campaign()
campaign.participant_reward = single_voucher_reward
campaign.save()

或者

campaign = Campaign(participant_reward = single_voucher_reward)
campaign.save()
于 2013-10-31T14:42:24.170 回答