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.

相关推荐
会飞的大可3 分钟前
Redis 竞品与替代方案选型可行性分析报告
数据库·redis·缓存
周杰伦的稻香9 分钟前
PostgreSQL基础命令
数据库·postgresql
Rabbit_QL21 分钟前
sklearn Pipeline:特征工程和建模流水线
人工智能·python·sklearn
MoRanzhi120322 分钟前
scikit-learn Lasso回归算法详解
python·机器学习·回归·scikit-learn·正则化·l1·lasso
先做个垃圾出来………24 分钟前
JSON序列化问题
数据库·json
财经资讯数据_灵砚智能24 分钟前
全球财经资讯日报(日间)2026年4月2日
大数据·人工智能·python·语言模型·ai编程
我科绝伦(Huanhuan Zhou)30 分钟前
InnoDB Undo Log 深度解析:从原理到实现(基于 MySQL 8.0)
数据库·mysql
Songgp102435 分钟前
yolo26+qwen3.5大小模型协同AI分析系统
图像处理·人工智能·python
jackiehome40 分钟前
SQL数据库无法操作,日志文件损坏修复
数据库·sql·oracle
人大博士的交易之路43 分钟前
数据结构算法——python数据结构
开发语言·数据结构·python