代码部分:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=30)
grade = models.IntegerField()
执行部分 :
python manage.py makemigrations
python manage.py migrate
django创建数据库表方法如下:
catcher:mysite catcher$ python manage.py makemigrations booksMigrations for 'books':
0001_initial.py:
- Create model Author
- Create model Book
- Create model Publisher
- Add field publisher to book
catcher:mysite catcher$ python manage.py sqlmigrate books 0001
BEGIN
--
-- Create model Author
--
CREATE TABLE "books_author" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "first_name" varchar(30) NOT NULL, "last_name" varchar(40) NOT NULL, "email" varchar(254) NOT NULL)
--
-- Create model Book
--
CREATE TABLE "books_book" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(100) NOT NULL, "publication_date" date NOT NULL)
CREATE TABLE "books_book_authors" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "book_id" integer NOT NULL REFERENCES "books_book" ("id"), "author_id" integer NOT NULL REFERENCES "books_author" ("id"))
--
-- Create model Publisher
--
CREATE TABLE "books_publisher" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "address" varchar(50) NOT NULL, "city" varchar(60) NOT NULL, "state_province" varchar(30) NOT NULL, "country" varchar(50) NOT NULL, "website" varchar(200) NOT NULL)
--
-- Add field publisher to book
--
ALTER TABLE "books_book" RENAME TO "books_book__old"
CREATE TABLE "books_book" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(100) NOT NULL, "publication_date" date NOT NULL, "publisher_id" integer NOT NULL REFERENCES "books_publisher" ("id"))
INSERT INTO "books_book" ("publication_date", "publisher_id", "id", "title") SELECT "publication_date", NULL, "id", "title" FROM "books_book__old"
DROP TABLE "books_book__old"
CREATE INDEX "books_book_2604cbea" ON "books_book" ("publisher_id")
COMMIT
catcher:mysite catcher$
catcher:mysite catcher$ sudo python manage.py migratePassword:Operations to perform: Apply all migrations: admin, contenttypes, books, auth, sessionsRunning migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying books.0001_initial... OK Applying sessions.0001_initial... OKcatcher:mysite catcher$
Django里面,管理数据库和sqlarchemy类似,也是通过orm框架来实现的。所有的数据库的建立,都是在model.py里面通过类来实现的。
首先看看如何创建一个单表:
a. 先定义一个类,继承models.Model, 然后根据需求定义参数,这些参数的类型和变量后面会进一步阐述
models.py
from django.db import models
class UserInfo(models.Model):
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)
b. 注册app
settings.py
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
‘app01‘,
]
c.执行命令。 第一条命令会生成一个初始化文件,第二个命令会生成对应的表
python manage.py makemigrations
python manage.py migrate
这样,就在PyCharm自带的sqlite数据库里面成功的生成了一个app01_UserInfo的表。这个表默认会有一个自增的id作为主键,另外两个字段是我们通过类创建的。
d. 如果希望使用mysql,因为Django默认使用了MySqldb模块,这个在3.0版本里面不存在,会直接报错。我们需要改为pymysql的模块,方法如下:
在project同名文件夹下的__init__文件中添加如下代码即可:
import pymysql
pymysql.install_as_MySQLdb()
2. 对于单表的增删改查询
查询
获取所有结果,获取到的结果是一个QuerySet的类似列表的对象,每一个元素本身又是一个对象,包括了id,name,password等属性。
obj = models.UserInfo.objects.all()
<QuerySet [<UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>, <UserInfo: UserInfo object>]>
可以通过filter进行过滤,相当于sql的where语句,因为结果也是QuerySet,因此需要再使用first()获取第一个值
obj = models.UserInfo.objects.filter(id=nid).first()
增加
models.UserInfo.objects.create(username=u,password=p,user_group_id=3)
删除,可以在filter的基础上进行删除
models.UserInfo.objects.filter(id=nid).delete()
修改,有两种常见方式
第一个方式
models.UserInfo.objects.filter(id=nid).update(username=u,password=p)
第二个方式
obj=models.UserInfo.objects.filter(id=nid)
obj.username=u
obj.save()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)