我在 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 条。
