假设我有一个具有主键的模型,我不希望它们的主键从1开始。有没有办法可以指定主键的初始值?
答案 0 :(得分:1)
class MyModel(models.Model):
primary_field = models.IntegerField(primary_key=True)
class Meta:
db_table = u'mymodel_seq'
def new_seq_number():
mymodel_seq = MyModel.objects.raw("select primary_field, nextval('mymodel_seq') from mymodel_seq")[0]
seq_number = u'%04i' % mymodel_seq.nextval
return seq_number