Django 向数据表中添加字段方法

在模型order中添加字段discount字段,并给予初始值0

方法

先在models.py中修改模型

添加

discount = models.DecimalField(max_digits=8,decimal_places=2, default=0)

然后,我们运行命令manage.py sqlall dish 来查看(dish是app名称):

CREATE TABLE "dish_order" (
    "id" integer NOT NULL PRIMARY KEY,
    "datetime" datetime NOT NULL,
    "customer_quantity" integer NOT NULL,
    "table" varchar(128),
    "seat" varchar(128),
    "state" varchar(128) NOT NULL,
    "discount" decimal NOT NULL,
    "discount_detail" text,
    "total_price" decimal,
    "reserved" varchar(128)
)
;   
执行如下语句
>>> from django.db import connection

>>> cursor = connection.cursor()

>>>cursor.execute('ALTER TABLE dish_order ADD COLUMN discount decimal DEFAULT 0') 

 <django.db.backends.sqlite3.base.SQLiteCursorWrapper at 0x947bc5c>

原文地址:https://www.cnblogs.com/nigang/p/3828589.html