题目描述
在django中创建两个模板实例后,删除后重新再创建一个模板实例后它的主键ID从3开始这是为什么?
怎样解决这个问题?
相关代码
modeles类:
from django.db import models
from django.contrib.auth.models import User
class BlogType(models.Model):
type_name = models.CharField(max_length=15)
def __str__(self):
return self.type_name
class Blog(models.Model):
title = models.CharField(max_length=50)
blog_type = models.ForeignKey(BlogType, on_delete=models.DO_NOTHING)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.DO_NOTHING)
created_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)
def __str__(self):
return "<Blog: %s>" % self.title
admin类:
@admin.register(BlogType)
class BlogTypeAdmin(admin.ModelAdmin):
list_display = ('id', 'type_name')
@admin.register(Blog)
class BlogAdmin(admin.ModelAdmin):
list_display = ('title', 'blog_type', 'author', 'created_time', 'last_updated_time')
这个django的orm没啥关系,mysql的自增主键本来就是这样的,你删除记录在进行insert,主键会从删除前的编号开始自增