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.

相关推荐
Devil枫24 分钟前
Kotlin扩展函数与属性
开发语言·python·kotlin
格调UI成品27 分钟前
预警系统安全体系构建:数据加密、权限分级与误报过滤方案
大数据·运维·网络·数据库·安全·预警
程序员阿超的博客1 小时前
Python 数据分析与机器学习入门 (八):用 Scikit-Learn 跑通第一个机器学习模型
python·机器学习·数据分析·scikit-learn·入门教程·python教程
xingshanchang2 小时前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
心平愈三千疾5 小时前
通俗理解JVM细节-面试篇
java·jvm·数据库·面试
费弗里5 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
学Linux的语莫9 天前
python基础语法
开发语言·python