2

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

4

1 回答 1

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
于 2013-03-08T08:16:12.193 回答