gpt4 book ai didi

python - Django:保存后ID为空

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:20 25 4
gpt4 key购买 nike

我在 models.py 中得到了这个模型:

class Boats(models.Model):
id = models.BigIntegerField(primary_key=True)
created = models.DateTimeField(default=timezone.now, blank=True)
name = models.CharField(max_length=30)

class Meta:
managed = True
db_table = 'boats'
ordering = ['name']

class Clients(models.Model):
id = models.BigIntegerField(primary_key=True)
created = models.DateTimeField(default=timezone.now, blank=True)
code = models.CharField(max_length=100)

class Meta:
managed = True
db_table = 'clients'
ordering = ['name']

==========================

在views.py中;我的功能是这样做的:

  f = NewBoatForm(request.POST)
if f.is_valid():
nBoat = f.save()
print 'ID:'+str(nBoat.id)
cBoat = ClientsBoats()
cBoat.client = client
cBoat.boat = nBoat
cBoat.save()

但 django 失败并出现此错误:

       ValueError: save() prohibited to prevent data loss due to unsaved related object 'boat'.

我打印了 ID,但它是 Null。谁能帮帮我。

最佳答案

您已将主键字段 id 覆盖为 AutoField 以外的内容。不要那样做。

通常你根本不会定义它,让 Django 自动完成。如果您真的确定标准整数字段不够大,您可以使用 BigAutoField。但我怀疑您的数据库中的船只数量是否超过 2147483647 条。

关于python - Django:保存后ID为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44913023/

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