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) (, 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.

相关推荐
程序员三藏21 小时前
Python+requests实现接口自动化测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
oradh21 小时前
Oracle闪回技术操作总结
数据库·oracle·oracle闪回技术操作总结·oracle闪回·flashback技术
ltl21 小时前
RocksDB 并发 Compaction 与 Rate Limiter
数据库
一直走下去-明1 天前
简单的http抓包解包完整代码
开发语言·python
雷帝木木1 天前
数据湖与数据仓库:从理论到实践
人工智能·python·深度学习·机器学习
数字化转型分享点滴1 天前
机加工车间上线 SH‑AIOT 物联网会遇到哪些常见实施难点
python·物联网
闻道且行之1 天前
图片处理助手|泊松融合原理 + C++ 工程实现,seamlessClone 三模式一次讲透
数据库·c++·人工智能·opencv
0566461 天前
agent学习——流式响应与文本切分
网络·python·学习
OPEN-F1 天前
Python进阶教程:正则表达式进阶与文本处理
开发语言·python·正则表达式
新网企兴1 天前
2026年陕西做GEO推广,找新网企兴解决获客难题
python·搜索引擎