【发布时间】:2013-08-26 14:38:56
【问题描述】:
我遇到了一些奇怪的行为,不太清楚为什么,我目前能够在另一个视图中毫无问题地做到这一点。
问题出在: 我正在创建一条新记录并将其保存到数据库中,但保存后它不会给我一个新记录的 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'
【问题讨论】:
-
r.pk 是否也返回 NULL?
-
是的,也试过了,也是 null
-
我知道这很老套,但是如果你先
r = r.save()然后r.pk会发生什么? -
您还有什么不告诉我们的吗?您是覆盖模型的
save方法,还是附加了任何post_save或pre_save信号处理程序? -
尝试从你的模型中删除这一行:id = models.IntegerField(primary_key=True)。
标签: django django-models django-queryset