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.

相关推荐
hnlucky17 分钟前
redis 数据类型新手练习系列——Hash类型
数据库·redis·学习·哈希算法
蓝莓味柯基27 分钟前
Python3:文件操作
python
xiaoh_71 小时前
解决视频处理中的 HEVC 解码错误:Could not find ref with POC xxx【已解决】
python·ffmpeg·音视频
LucianaiB1 小时前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
时序数据说1 小时前
时序数据库IoTDB在航空航天领域的解决方案
大数据·数据库·时序数据库·iotdb
明月与玄武2 小时前
Python编程的真谛:超越语法,理解编程本质
python·编程语言
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
AnsenZhu2 小时前
2025年Redis分片存储性能优化指南
数据库·redis·性能优化·分片
oydcm2 小时前
MySQL数据库概述
数据库·mysql