django如何更新数据库字段并与数据库保持同步?

关键步骤:

  1. 第一步: 执行:python manage.py makemigrations 你的项目名称

  2. 第二步:它会提示你选1还是2,这里因为添加字段,所以选1

  3. 第三步:出现>>>这个,直接输入这个''

  4. 第四步: 执行:python manage.py migrate

  5. 第五步:运行项目:python manage.py runserver 本机ip:8000

    (.venv) PS D:\python_workpace\django_xitong_shezhi\pythonProject\myproject> python manage.py makemigrations myproject
    It is impossible to add a non-nullable field 'title_field22' to yingyongguanli without specifying a default. This is because the database needs something to populate existing rows.
    Please select a fix:
    2) Quit and manually define a default value in models.py.
    Select an option: 1
    Please enter the default value as valid Python.
    The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.

    ''
    Invalid input: invalid character ''' (U+2018) (<string>, line 1)
    ''
    Migrations for 'myproject':
    myproject\migrations\0002_yingyongguanli_title_field22.py

     - Add field title_field22 to yingyongguanli
    

    (.venv) PS D:\python_workpace\django_xitong_shezhi\pythonProject\myproject> python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, myproject, sessions
    Running migrations:
    Applying myproject.0002_yingyongguanli_title_field22... OK
    (.venv) PS D:\python_workpace\django_xitong_shezhi\pythonProject\myproject> python manage.py runserver 10.0.4.144:8000
    Watching for file changes with StatReloader
    Performing system checks...

    System check identified no issues (0 silenced).
    August 07, 2024 - 15:05:12
    Django version 5.0.6, using settings 'myproject.settings'
    Starting development server at http://10.0.4.144:8000/
    Quit the server with CTRL-BREAK.

相关推荐
羱滒4 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
程序员Android7 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师8 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
甜可儿19 分钟前
redis序列化设置
数据库·redis
GGBondlctrl24 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
若云止水38 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
紫雾凌寒38 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
千层冷面2 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存