我遇到了一些奇怪的行为,不太清楚为什么,我目前能够在另一个视图中毫无问题地做到这一点。
这是问题所在: 我正在创建一条新记录并将其保存到数据库中,但是在保存后它不会给我一个新记录的 ID,只是 NULL,即使该记录正被插入到数据库中而没有错误。
我的代码:
r = KsProjectRewards()
r.project_id = project_id
r.reward_description_short = desc_short
r.reward_description_long = desc_long
r.limit_max = limit
r.international_info = international
r.is_active = active
r.save()
r.id # <!-- THIS RETURNS NULL
这是我的模型
class KsProjectRewards(models.Model):
id = models.IntegerField(primary_key=True)
project_id = models.IntegerField()
reward_description_short = models.CharField(max_length=75L, blank=True)
reward_description_long = models.CharField(max_length=500L, blank=True)
num_backers = models.IntegerField(null=True, blank=True)
limit_count = models.IntegerField(null=True, blank=True)
limit_max = models.IntegerField(null=True, blank=True)
limit_met = models.IntegerField(null=True, blank=True)
delivery_date = models.DateTimeField(null=True, blank=True)
international_info = models.CharField(max_length=200L, blank=True)
date_added = models.DateTimeField()
date_updated = models.DateTimeField(null=True, blank=True)
is_active = models.IntegerField()
class Meta:
db_table = 'ks_project_rewards'