当前位置:网站首页>Auto-created primary key used when not defining a primary key
Auto-created primary key used when not defining a primary key
2022-06-30 21:08:00 【weixin_888988】
When you define a model in Django without specifying a primary key, Django will automatically create a primary key for you. The primary key is set to be an integer. If you want to override the field type, you can do so on a per model basis.
Starting in Django 3.2, you can now customise the type of the automatically created primary key in your settings.
Starting new projects in Django 3.2, the default type for primary keys is set to a BigAutoField which is a 64 bit integer. However, earlier versions set the type of implicit primary keys to be integers.
This means that when you upgrade to 3.2, you will start to see warnings about the fact that you do not have an explicitly defined primary key type. Satisfying Django's requirements for an explicitly set primary key type is easy enough, but you also need to choose whether or not you want to upgrade your primary key field types from integer to 64 bit integer.
There are a few ways to fix this. Broadly speaking they fall into two categories
Configure DEFAULT_AUTO_FIELD in settings
Open your project's settings.py and add a new line at the bottom of the file
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
Setting on a per app basis
If you prefer to set your field type on a per app basis rather than for the whole project, you can specify this in apps.py.
from django.apps import AppConfig
class MyappConfig(AppConfig):
default_auto_field = 'django.db.models.AutoField'
name = 'Myapp'
Set AutoField or BigAutoField on a per model basis
If you prefer even more fine grained control, you can set a per model id field.
class Model1(models.Model):
id = models.BigAutoField(primary_key=True)边栏推荐
猜你喜欢

uniapp-路由uni-simple-router

Software engineering UML drawing

Study on lumiprobe dye NHS ester BDP FL NHS ester

偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的

stacking集成模型预测回归问题

Apply for vector bus protocol color picture wallpaper hanging picture, very good!

Study on PEGylation of lumiprobe and PEG linker - iodine-peg3-acid

go搭建服务器基础

Deflection lock / light lock / heavy lock lock is healthier. How to complete locking and unlocking

PHP obtains opcode and C source code
随机推荐
WebRTC系列-网络传输之本地scoket端口
注册设备监理师难考吗,和监理工程师有什么关系?
Flutter 嵌套地狱?不存在的,ConstraintLayout 来解救!
stacking集成模型预测回归问题
报错:Internal error XFS_WANT_CORRUPTED_GOTO at line 1635 of file fs/xfs/libxfs/xfs_alloc.c.
阿里kube-eventer mysql sink简单使用记录
SQL Server 提取字符串中的纯数字
【微服务~Nacos】Nacos之配置中心
报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
Scene 299
B_QuRT_User_Guide(32)
Lumiprobe 聚乙二醇化和 PEG 接头丨碘-PEG3-酸研究
Study on lumiprobe modified triphosphate biotin-11-utp
文本识别-SVTR论文解读
Adobe Photoshop (PS) - script development - remove file bloated script
Markdown笔记简明教程
在线教育项目用户登录和注册
毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
两个skyline
修改已经上线的小程序名称