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.

相关推荐
geovindu3 分钟前
python: Simple Factory Pattern
开发语言·python·设计模式·简单工厂模式
JuneXcy4 分钟前
第9章 关系模式的规范化设计理论
数据库·mysql
weixin1997010801613 分钟前
开山网商品详情页前端性能优化实战
java·前端·python
前端付豪19 分钟前
实现学习报告统计面板
前端·python·llm
AC赳赳老秦23 分钟前
国产化AI运维新趋势:DeepSeek赋能国产算力部署的高效故障排查
大数据·人工智能·python·django·去中心化·ai-native·deepseek
1941s23 分钟前
01-LLM 基础与提示词工程:从 API 调用到 Prompt 优化技巧
人工智能·python·prompt
larance24 分钟前
NebulaGraph 数据库部署与运维指令清单
linux·服务器·数据库
想不明白的过度思考者28 分钟前
【MyBatis 知识点解析】#{} 与 ${} 的区别及 SQL 注入实战演示
java·数据库·spring boot·sql·mybatis
python猿28 分钟前
打卡Python王者归来--第28天
python
itwangyang52028 分钟前
GitHub Push Protection 报错解决指南(检测到 Token / Secret)
人工智能·python·github