当前位置:网站首页>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)
边栏推荐
猜你喜欢
ArcGIS construction and release of simple road network data service and rest call test
MySQL advanced 3
Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
PHP obtains opcode and C source code
多表操作-外键约束
CentOS - enable / disable Oracle
Binary search tree (1) - concept and C language implementation
Adobe Photoshop (PS) - script development - remove file bloated script
Study on lumiprobe modified triphosphate biotin-11-utp
ArcGIS构建发布简单路网Network数据服务及Rest调用测试
随机推荐
修改已经上线的小程序名称
DM8:生成DM AWR报告
How can I get the stock account opening discount link? In addition, is it safe to open a mobile account?
Double solid histogram / double y-axis
Peking University ACM problems 1004:financial management
等级测评是什么意思?工作流程包含哪些?
毕业设计
uniapp-第三方网络请求
多态在代码中的体现
Basic concepts of tree
Peking University ACM problems 1001:exposition
What are database OLAP and OLTP? Same and different? Applicable scenarios
Lvalue reference and lvalue reference
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
Web APIs 综合案例-Tab栏切换 丨黑马程序员
【微服务~Nacos】Nacos之配置中心
Game 81 biweekly
How do I get the largest K massive data
雷达数据处理技术