当前位置:网站首页>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)边栏推荐
- MySQL高级篇3
- Dynamic style binding --style and class
- Lumiprobe protein quantitation - qudye Protein Quantitation Kit
- 防范未授权访问攻击的十项安全措施
- 毕业五年,想当初若没有入行测试,我是否还会如这般焦虑
- 代码改变一小步,思维跨越一大步
- uniapp怎么上传二进制图片
- Two skylines
- What about degradation of text generation model? Simctg tells you the answer
- Peking University ACM problems 1002:487-3279
猜你喜欢

MySQL advanced 3

3Ds Max 精模obj模型导入ArcGIS Pro (二)要点补充

凤凰架构——架构师的视角

uniapp-富文本编辑器

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

报错:Internal error XFS_WANT_CORRUPTED_GOTO at line 1635 of file fs/xfs/libxfs/xfs_alloc.c.

BioVendor sRAGE Elisa试剂盒测试原理和注意事项

MySQL高级篇3

RP原型资源分享-购物类App

报错FileSystemException: /datas/nodes/0/indices/gtTXk-hnTgKhAcm-8n60Jw/1/index/.es_temp_file:结构需要清理
随机推荐
在线教育项目用户登录和注册
【数字IC应届生职业规划】Chap.1 IC行业产业链概述及代表企业大厂汇总
Dynamic style binding --style and class
Lumiprobe copper free click chemical solution
Lumiprobe 改性三磷酸盐丨生物素-11-UTP研究
偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的
Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse
Lumiprobe核酸定量丨QuDye dsDNA BR 检测试剂盒
19.04 distributor
Huffman tree (I) basic concept and C language implementation
Markdown笔记简明教程
MySQL advanced 3
Peking University ACM problems 1004:financial management
On inline function
C file pointer
ICLR'22 Spotlight | 怎样度量神经网络权重中的信息量?
Qiao NPMS: search for NPM packages
SqlServer 获取字符串中数字,中文及字符部分数据
两个skyline
大学生研究生毕业找工作,该选择哪个方向?