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

时间:2013-10-31 14:36:52

标签: python django django-models

我在尝试保存Campaign时收到错误消息'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')

1 个答案:

答案 0 :(得分:2)

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

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

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