django第六课 模型第一讲
1.数据库连接配置:
django 连接mysql的配置流程:
- 安装 pymysql pip install pymysql
- 创建数据库用户
有创建数据库权限的用户
- 创建数据库
可以通过navicat来创建,或者直接用命令创建
create database crm
- 修改配置
修改settings文件中的
1 DATABASES = { 2 'default': { 3 'ENGINE': 'django.db.backends.mysql', 4 'NAME': 'crm', 5 'USER':'root', 6 'PASSWORD':'', 7 'HOST':'***', 8 'PORT':'3306' 9 } 10 }
- 修改项目文件夹(和settings.py文件所在的目录)下
__init__.py 文件
写上:
1 import pymysql 2 pymysql.install_as_MySQLdb()
- 设置时区 TIME_ZONE = 'Asia/ShangHai' # 北京时间
2.django的ORM系统
-对象关系映射(Object Relational Mapping,简称ORM)! 简单的说就是用面向对象的方式,描述数据库,操作数据库,
达到不用编写SQL语句就能对数据库进行增删改查。
3.模型的创建与激活创建模型
Student的模型,代表啊学生 总结:
1. 每一个模型都是django.db.models.Model的子类
2. 类变量 表示模型中的数据库字段
3. 每一个字段由一个字段类的实例表示
激活模型
1.在项目中注册app
2.运行数据库迁移命令(一定要在项目根目录下)
python mange.py makemigrations teacher
告诉django,我们做了哪些数据库的更改
迁移
sqlmigrate 从迁移获取sql语句
1 (django) [root@iZuf68mx592121gk7l1ca1Z crm]# python3 manage.py sqlmigrate teacher 0001 2 BEGIN; 3 -- 4 -- Create model Student 5 -- 6 CREATE TABLE `teacher_student` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(20) NOT NULL, `age` smallint NOT NULL, `sex` smallint NOT NULL, `qq` varchar(20) NOT NULL, `phone` varchar(20) NOT NULL, `c_time` datetime(6) NOT NULL); 7 COMMIT;
3.运行migrate命令,使迁移生效
python manage.py migrate teacher
# 表名 appname_模型name.lower
4.数据的增删改查
增:
第一种方法:
In [10]: s1 Out[10]: <Student: Student object (None)> In [11]: s1 = Student(name = 'donghao',age=25,qq = '11111') In [12]: s1.save() In [13]: Student.objects.all() Out[13]: <QuerySet [<Student: Student object (1)>]>
第二种方法:
1 In [14]: s2 = Student() 2 3 In [15]: s2.name = 'xinlan' 4 5 In [16]: s2.age = 30 6 7 In [17]: s2.save() 8 9 In [18]: Student.objects.all() 10 Out[18]: <QuerySet [<Student: Student object (1)>, <Student: Student object (2)>]>
mysql> select * from teacher_student -> ; +----+---------+-----+-----+-------+-------+----------------------------+ | id | name | age | sex | qq | phone | c_time | +----+---------+-----+-----+-------+-------+----------------------------+ | 1 | donghao | 25 | 1 | 11111 | | 2019-02-22 15:17:07.621733 | | 2 | xinlan | 30 | 1 | | | 2019-02-22 15:19:38.728134 | +----+---------+-----+-----+-------+-------+----------------------------+ 2 rows in set (0.00 sec)
第三种方法:
In [7]: Student.objects.create(name='weigang',age=22) Out[7]: <Student: weigang-22
第四种方法:
In [8]: Student.objects.all() Out[8]: <QuerySet [<Student: donghao-25>, <Student: xinlan-30>, <Student: weigang-22>]> In [9]: s = Student.objects.get_or_create(name = 'moran') In [10]: s Out[10]: (<Student: moran-0>, True) In [11]: s = Student.objects.get_or_create(name = 'donghao') In [12]: s Out[12]: (<Student: donghao-25>, False)
返回的是一个元祖
false 代码查询到的
true 代表创建的
查询:
查询所有的
In [13]: Student.objects.all()
Out[13]: <QuerySet [<Student: donghao-25>, <Student: xinlan-30>, <Student: weigang-22>, <Student: moran-0>]>
调用的sql语句
In [15]: print(res.query) SELECT `teacher_student`.`id`, `teacher_student`.`name`, `teacher_student`.`age`, `teacher_student`.`sex`, `teacher_student`.`qq`, `teacher_student`.`phone`, `teacher_student`.`c_time` FROM `teacher_student`
查询一条数据:
In [16]: Student.objects.get(id=1)
Out[16]: <Student: donghao-25>
In [17]: Student.objects.get(pk=1)
Out[17]: <Student: donghao-25>
查询多条数据
In [18]: res = Student.objects.filter(sex=1) In [19]: res Out[19]: <QuerySet [<Student: donghao-25>, <Student: xinlan-30>, <Student: weigang-22>, <Student: moran-0>]
改:
改一条数据:
In [20]: s =Student.objects.get(name='xinlan') In [21]: s Out[21]: <Student: xinlan-30> In [22]: s.age = 16 In [23]: s Out[23]: <Student: xinlan-16>
另外一种方法:
In [26]: Student.objects.filter(name='xinlan').update(age=18) Out[26]: 1 In [27]: Student.objects.get(name='xinlan') Out[27]: <Student: xinlan-18>
删除
In [31]: s = Student.objects.get(pk=2) In [32]: s Out[32]: <Student: xinlan-18> In [33]: s.delete() Out[33]: (1, {'teacher.Student': 1})
批量删除:
In [36]: Student.objects.filter(sex=1).delete() Out[36]: (3, {'teacher.Student': 3})