Auto-created primary key used when not defining a primary key

2023-05-16

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Auto-created primary key used when not defining a primary key 的相关文章

随机推荐

  • 服务器散热器性能,服务器散热器

    服务器散热器 内容精选 换一换 安装run包时 xff0c 提示固件升级失败 xff0c 并且日志中打印信息显示未找到昇腾AI处理器 xff0c 如图1所示 服务器上昇腾处理器的标卡未插紧 接触不良或通风散热不好 通过指令lspci gre
  • 主机中修改docker 容器中的配置文件

    前言 最近在学习docker 过程中遇到这样一个问题 xff1a 在mysql容器中修改了mysql相关的配置 xff0c 重启mysql 服务时 xff0c docker中的mysql容器会关闭 这时候使用docker start 容器名
  • 打不开磁盘...或它所依赖的某个快照磁盘

    这主要是非正常关虚拟机造成的 xff0c 具体原因如下 xff1a 虚拟机为了防止有多虚拟机共用一个虚拟磁盘 xff08 就是后 缀为 vmdk那个文件 xff09 造成数据的丢失和性能的削弱 xff0c 每次启动虚拟机时会给每个虚拟磁盘加
  • shiro-cas------自定义登录页面

    我的自定义登录页 xff08 需要登录页面的 xff0c 推荐给你们一个登陆页面地址 xff09 我的项目结构 xff1a 学习过程参考官方文档https apereo github io cas 5 3 x installation Us
  • docker安装nacos

    这里我们安装单机nacos集群 Nacos的单节点模式 standalone xff0c 配置的数据是默认存储到内嵌的数据库derby中 如果我们要搭建集群的话 xff0c 数据需要共享 xff0c 此时内嵌数据库无法满足 xff0c 需要
  • Java从控制端输入一个未知长度的数组

    String str 61 sc next toString String arr 61 str split 34 34 int b 61 new int arr length for int j 61 0 j lt b length j
  • win10 安装db2 10.1 并使用DBserver连接db2数据库

    系统 xff1a win10 64 专业版 db2下载 xff1a 链接 xff1a https pan baidu com s 1IiAUdRUTIDcGAew7WbVTHQ 提取码 xff1a wzpg dbServer 链接 xff1
  • win10 安装 cognos 10.2.0

    安装包链接 xff1a 链接 xff1a https pan baidu com s 1z yMlvjd1fMHXOv gbCc8A 提取码 xff1a jcfd 这里我安装到d盘cognos下 bi svr 32b 10 2 win ml
  • CentOS 7的安装

    访问其官网 xff0c https www centos org xff0c 点击Get CentOS Now xff0c 点击alternative downloads xff0c 点击CentOS 7列表中的x86 64 xff0c 点
  • PowerDesigner16.5汉化破解版安装教程(含安装文件、汉化包、破解文件)

    一 软件安装 1 下载安装包 xff08 包含安装文件 汉化包 破解文件 xff09 xff0c 下载链接在文章最后 xff0c 失效请留言 2 下载后文件内容如下 3 进入安装文件中双击安装文件等待初始化完成后选择next 4 继续下一步
  • 数据库设计——评论回复功能

    1 概述 评论功能已经成为APP和网站开发中的必备功能 本文主要介绍评论功能的数据库设计 评论功能最主要的是发表评论和回复评论 xff08 删除功能在后台 xff09 评论功能的拓展功能体现有以下几方面 xff1a xff08 1 xff0
  • 问答社区竞品分析——知乎与悟空的较量

    1 产品定位 知乎 精英化 知乎的产品定位是知识分享性的社区平台 xff0c 面向各行业精英人群和广大网友 属于知乎大V的精英人群在此发表自己的见解 xff0c 寻找精神上的认同和物质上的奖励 而广大网友在此获得感兴趣的知识或作为娱乐消遣的
  • VirtualBox搭建CenterOS7-Docker,实现IntelliJ IDEA部署Springboot Docker镜像

    服务器安装 Docker 首选安装在Linux系统上 xff0c xff08 有钱的大佬可以直接在阿里云买服务器 xff0c 可以省略这个步骤 xff09 开始为了偷懒在 win7上安装了DockerToolbox xff08 win10安
  • 757计算机电子元件,飞行员的好帮手 波音757的发动机指示与机组报警系统简介...

    原标题 xff1a 飞行员的好帮手 波音757的发动机指示与机组报警系统简介 陈光 文 在以往的飞机中 需要驾驶员监测的发动机参数均是利用驾驶舱仪表板上的电子 机械仪表来显示的 再用一些声 光告警装置在飞机 发动机的某些系统与元件出现故障或
  • linux crontab 每隔10秒执行一次

    linux下定时执行任务的方法 在LINUX中你应该先输入crontab e xff0c 然后就会有个vi编辑界面 xff0c 再输入0 3 1 clearigame2内容到里面 wq 保存退出 在LINUX中 xff0c 周期执行的任务一
  • 生命的轨迹会沿着期望的方向走去

    生命的轨迹会沿着期望的方向走去 平凡的世界 飘 巴黎圣母院 我的苦难我的大学 活着 幸福了吗 痛并快乐着 大学期间 xff1a 狼图腾 我的大学 高尔基 读大学该读什么 霍乱时期的爱情 百年孤独 断舍离 穆斯林的葬礼 宋庆龄传 西班牙旅游日
  • MySQL [Err] 1241 - Operand should contain 1 column(s)

    Operand should contain 1 column s 翻译过来就是 xff1a 操作数应包含一列 xff1b 错误原因 往往是我们多出一列操作数 xff0c 或者给的参数格式不正确 xff1b 解决方法 xff1a Demo1
  • docker-compose开机自启动设置

    前提 需要设置docker开机自启动 xff1a systemctl enable docker docker compose开机自启动两种方式 第一种方式 主要步骤如下 xff1a xff08 1 xff09 创建docker compo
  • NFS使用详解之三.NFS传输速度优化

    十 nfs的传输速度优化 如果按 mount o nolock 192 168 1 220 假设为宿主机ip mnt nfs mnt nfs t 来mount xff0c 传输速度可能很慢 xff0c 只有几K到几十K左右 xff0c 所以
  • Auto-created primary key used when not defining a primary key

    When you define a model in Django without specifying a primary key Django will automatically create a primary key for yo