当前位置:网站首页>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:11:00 【weixin_ eight hundred and eighty-eight thousand nine hundred an】
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)边栏推荐
- Binary search tree (1) - concept and C language implementation
- B_QuRT_User_Guide(34)
- 开发技术-使用easyexcel导入文件(简单示例)
- [original] unable to use the code dialog height and width to solve the screen problem
- ICLR'22 Spotlight | 怎样度量神经网络权重中的信息量?
- Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
- Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
- Dynamic style binding --style and class
- 代码改变一小步,思维跨越一大步
- Amazon restricts LGBTQ related search and product sales in the United Arab Emirates
猜你喜欢

Lumiprobe dye hydrazide - BDP FL hydrazide solution

雷达数据处理技术

MySQL简介、详细安装步骤及使用 | 黑马程序员

Lumiprobe biotin phosphimide (hydroxyproline) instructions

MySQL:SQL概述及数据库系统介绍 | 黑马程序员

MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等

Double solid histogram / double y-axis

个人开发的渗透测试工具Satania

Oracle 数据库表结构 Excel 导出

Introduction of 3D Max fine model obj model into ArcGIS pro (II) key points supplement
随机推荐
Label Contrastive Coding based Graph Neural Network for Graph Classification
时空数据挖掘:综述
ArcGIS构建发布简单路网Network数据服务及Rest调用测试
Text recognition svtr paper interpretation
3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充
为什么vscode用久了电脑速度变慢?
代码改变一小步,思维跨越一大步
uniapp-第三方网络请求
Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
注册设备监理师难考吗,和监理工程师有什么关系?
Go build server Foundation
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
多态在代码中的体现
B_QuRT_User_Guide(31)
毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
在线教育项目用户登录和注册
MySQL高级篇3
Peking University ACM problems 1000:a+b problem
Oracle 数据库表结构 Excel 导出
B_QuRT_User_Guide(32)