我的理解, "Default value for primary key is provided"意思是在 model 建立时的主键字段有 default=xxx, 例如
Class Order(models.Model):
id=models.AutoField('ID', primary_key=True, auto_created=True, default=True)
status = models.PositiveSmallIntegerField(default=1)
测试代码:
>>> a=Order()
>>> a.save()
>>> a
<Order: Order object (1)>
>>>
a.id1
>>> b=Order()
>>> b.save()
Traceback (most recent call last):
...
sqlite3.IntegrityError: UNIQUE constraint failed:
order_order.id>>>
b.id=2
>>> b.save()
>>> a,b
(<Order: Order object (1)>, <Order: Order object (2)>)
但是 id 字段的 default value 是 True,似乎没什么意思,可以改进为如下的定义:
import time
from django.db import models
def get_pk():
return int(time.time() * 10000)
class Order(models.Model):
id=models.AutoField('ID', primary_key=True, auto_created=True, default=get_pk)
status = models.PositiveSmallIntegerField(default=1)
测试代码:
>>> c=Order()
>>> c.save()
>>>
c.id15816669672836
>>> d=Order()
>>> d.save()
>>>
d.id15816669758912
-- 完毕 --