如何在Django中设置主键最小值

时间:2013-03-08 08:03:35

标签: django

假设我有一个具有主键的模型,我不希望它们的主键从1开始。有没有办法可以指定主键的初始值?

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