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.

相关推荐
ptc学习者19 小时前
OGG 安装注意事项
java·开发语言·数据库
鸽鸽程序猿19 小时前
【MySQL】索引
数据库·mysql
zym大哥大20 小时前
Redis-Zest
数据库·redis·缓存
该用户已不存在20 小时前
Python项目的5种枚举骚操作
后端·python
mortimer20 小时前
从 Python+venv+pip 迁移到 uv 全过程 及 处理 torch + cuda 的跨平台指南
pytorch·python·macos
berryyan20 小时前
Windows WSL 环境下配置 Claude Code 非官方账号2233.ai完整教程
人工智能·python
zl97989920 小时前
Redis-stream、bitfield类型
数据库·redis·缓存
用户83562907805120 小时前
告别冗余:用Python删除PDF中的超链接
后端·python
数据库那些事儿20 小时前
Qoder + ADB Supabase :5分钟GET超火AI手办生图APP
数据库·后端
IvorySQL20 小时前
PostgreSQL 18 异步 I/O(AIO)调优指南
数据库·postgresql