Django添加数据库字段

想在模型essay中添加字段view_count,并给予初始值0

方法

先在models.py中修改模型

添加

view_count=models.IntegerField(verbose_name=’浏览次数’,default=0)

然后,我们运行命令manage.py sqlall core来查看,以下是其中一段:

CREATE TABLE “core_essay” (
“id” integer NOT NULL PRIMARY KEY,
“eType_id” integer NOT NULL REFERENCES “core_eassaytype” (“id”),
“title” varchar(25) NOT NULL,
“content” text NOT NULL,
“abstract” text NOT NULL,
“pub_date” datetime NOT NULL,
“view_count” integer NOT NULL
)
;执行如下语句

>>> from django.db import connection

>>> cursor = connection.cursor()

>>>cursor.execute(‘ALTER TABLE core_essay ADD COLUMN view_count integer DEFAULT 0’)

<django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x020C31C0>

 

发表评论

邮箱地址不会被公开。 必填项已用*标注