一、创建django项目
1、进入虚拟环境创建django项目
cd Python\envdjango01\Scripts
activate
cd ..
#创建django项目
django-admin startproject django03
2、新建app
(envdjango01) D:\Python\envdjango01\django03>manage.py startapp MyApp
--settings.py
#注册app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'MyApp'
]
# Create your models here.--models.py
#学校模型
#班级模型
#学生模型
#数学模型
class School(models.Model):
name = models.CharField(max_length=128)
age = models.IntegerField()
addrees = models.TextField()
class Classes(models.Model):
c_id = models.CharField(max_length=64)
c_grade = models.IntegerField()
c_teacher = models.CharField((max_length=64)
c_monitor = models.CharField(max_length=32)
class Student(models.Model):
name = models.CharField(max_length=8)
age = models.IntegerField(default=18)
#true代表男同学 false代表女同学
sex = models.BooleanField()
moblie = models.CharField(max_length=16)
no = models.IntegerField()
class Teacher(models.Model):
name = models.CharField(max_length=8)
age = models.IntegerField(default=18)
#true代表男同学 false代表女同学
sex = models.BooleanField()
moblie = models.CharField(max_length=16)
no = models.IntegerField()
salary = models.FloatField()
#制作迁移文件
(envdjango01) D:\Python\envdjango01\django03>manage.py makemigrations MyApp
Migrations for 'MyApp':
MyApp\migrations\0001_initial.py
- Create model Classes
- Create model School
- Create model Student
- Create model Teacher
--settings.py
#配置数据库
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django03',
'USER':'root',
'PASSWORD':'123456',
'HOST':'localhost'
}
}
#进入数据库
C:\Windows\system32>mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#创建数据库django03
mysql> create database django03;
Query OK, 1 row affected (0.08 sec)
#执行迁移
(envdjango01) D:\Python\envdjango01\django03>manage.py migrate MyApp
Operations to perform:
Apply all migrations: MyApp
Running migrations:
Applying MyApp.0001_initial... OK
二、ipython交互查询数据库数据
条件判断
filter;select * from table_name where .....
all: select * from table_name;查询全部内容
filter(name='xx',age=10):select * from table_name where name='xx' and age=10
exclude(...) select * from table_name where not(name='xxx' and age=10);
get(pk=1): 必须返回一个对象,假如数据中找不到对应的数据,那么就会抛出异常;假如数据中有多条数据满足条件,那么也会抛出异常
复杂查询
**条件判断**
- __gt:大于 >
- __gte:大于等于 >=
- __lt:小于 <
- __lte:小于等于 <=
- __contains:包含 %value%
- __icontains:忽略大小写包含
- __endswith:以某字符串结尾 %value
- __iendswith:忽略大小写
- __startswith:以某字符串开始 value%
- __istartswith:忽略大小写
- __in:是否属于某一个数组 in ()
#安装ipython
(envdjango01) D:\Python\envdjango01\django03>pip install ipython
#进入shell交互工具 进行测试数据库用的
(envdjango01) D:\Python\envdjango01\django03>manage.py shell
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
#ipython创建查询数据
In [3]: from MyApp.models import *
In [4]: sc1 = School()
In [5]: sc1.name = '千锋'
In [6]: sc1.age = 20
In [7]: sc1.addrees = '北京宝生里'
In [8]: sc1.save()
In [9]: School.objects.filter(name='千锋')
Out[9]: <QuerySet [<School: School object (1)>]>
In [10]: sces = School.objects.filter(name='千锋')
In [11]: sces
Out[11]: <QuerySet [<School: School object (1)>]>
In [12]: sc = sces[0]
In [13]: sc
Out[13]: <School: School object (1)>
In [14]: sc.name
Out[14]: '千锋'
In [15]: sc.age
Out[15]: 20
In [16]: sc.addrees
Out[16]: '北京宝生里'
In [17]: sc.id
Out[17]: 1
#创建查询数据2
In [18]: sc2 = School()
In [19]: sc2.name = '清华'
In [20]: sc2.age = 100
In [21]: sc2.addrees = '西安'
In [22]: sc2.save()
In [23]: sces = School.objects.filter(name='清华')
In [24]: sces
Out[24]: <QuerySet [<School: School object (2)>]>
In [25]: sc = sces[0]
In [26]: sc.name
Out[26]: '清华'
In [27]: sc.age
Out[27]: 100
In [28]: sc.addrees
Out[28]: '西安'
In [29]: sc.id
Out[29]: 2
#更新数据库
In [36]: School.objects.filter(name='清华大学')[0].name
Out[36]: '清华大学'
#删除数据
In [38]: sc.delete()
Out[38]: (1, {'MyApp.School': 1})
In [39]: School.objects.filter(name='清华大学')
Out[39]: <QuerySet []>
#随机创建多个对象
In [40]: import random
In [41]: for i in range(100):
...: sc=School()
...: sc.name='school_' +str(i)
...: sc.age = random.randint(1,50)
...: sc.address = 'address_' +str(i)
...: sc.save()
...:
In [42]:
#查看数据库中数据
mysql> use django03;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_django03 |
+--------------------+
| django_migrations |
| myapp_classes |
| myapp_school |
| myapp_student |
| myapp_teacher |
+--------------------+
5 rows in set (0.00 sec)
mysql> select * from MyApp_school;
+-----+-----------+-----+-----------------+
| id | name | age | addrees |
+-----+-----------+-----+-----------------+
| 1 | 千锋 | 20 | 北京宝生里 |
| 3 | school_0 | 4 | |
| 4 | school_1 | 14 | |
| 5 | school_2 | 12 | |
| 6 | school_3 | 46 | |
| 7 | school_4 | 16 | |
| 8 | school_5 | 2 | |
| 9 | school_6 | 2 | |
| 10 | school_7 | 26 | |
| 11 | school_8 | 10 | |
| 12 | school_9 | 2 | |
| 13 | school_10 | 35 | |
| 14 | school_11 | 11 | |
| 15 | school_12 | 48 | |
| 16 | school_13 | 2 | |
| 17 | school_14 | 21 | |
| 18 | school_15 | 40 | |
| 19 | school_16 | 32 | |
| 20 | school_17 | 10 | |
| 21 | school_18 | 17 | |
| 22 | school_19 | 34 | |
| 23 | school_20 | 5 | |
| 24 | school_21 | 43 | |
| 25 | school_22 | 47 | |
| 26 | school_23 | 23 | |
| 27 | school_24 | 25 | |
| 28 | school_25 | 26 | |
| 29 | school_26 | 34 | |
| 30 | school_27 | 38 | |
| 31 | school_28 | 3 | |
| 32 | school_29 | 11 | |
| 33 | school_30 | 31 | |
| 34 | school_31 | 43 | |
| 35 | school_32 | 10 | |
| 36 | school_33 | 28 | |
| 37 | school_34 | 36 | |
| 38 | school_35 | 23 | |
| 39 | school_36 | 11 | |
| 40 | school_37 | 3 | |
| 41 | school_38 | 38 | |
| 42 | school_39 | 3 | |
| 43 | school_40 | 36 | |
| 44 | school_41 | 20 | |
| 45 | school_42 | 33 | |
| 46 | school_43 | 25 | |
| 47 | school_44 | 41 | |
| 48 | school_45 | 9 | |
| 49 | school_46 | 24 | |
| 50 | school_47 | 3 | |
| 51 | school_48 | 1 | |
| 52 | school_49 | 23 | |
| 53 | school_50 | 28 | |
| 54 | school_51 | 13 | |
| 55 | school_52 | 13 | |
| 56 | school_53 | 2 | |
| 57 | school_54 | 31 | |
| 58 | school_55 | 32 | |
| 59 | school_56 | 2 | |
| 60 | school_57 | 7 | |
| 61 | school_58 | 31 | |
| 62 | school_59 | 34 | |
| 63 | school_60 | 33 | |
| 64 | school_61 | 25 | |
| 65 | school_62 | 49 | |
| 66 | school_63 | 27 | |
| 67 | school_64 | 36 | |
| 68 | school_65 | 11 | |
| 69 | school_66 | 7 | |
| 70 | school_67 | 5 | |
| 71 | school_68 | 29 | |
| 72 | school_69 | 2 | |
| 73 | school_70 | 30 | |
| 74 | school_71 | 1 | |
| 75 | school_72 | 31 | |
| 76 | school_73 | 9 | |
| 77 | school_74 | 13 | |
| 78 | school_75 | 34 | |
| 79 | school_76 | 23 | |
| 80 | school_77 | 4 | |
| 81 | school_78 | 9 | |
| 82 | school_79 | 9 | |
| 83 | school_80 | 19 | |
| 84 | school_81 | 30 | |
| 85 | school_82 | 2 | |
| 86 | school_83 | 19 | |
| 87 | school_84 | 37 | |
| 88 | school_85 | 40 | |
| 89 | school_86 | 16 | |
| 90 | school_87 | 17 | |
| 91 | school_88 | 40 | |
| 92 | school_89 | 16 | |
| 93 | school_90 | 17 | |
| 94 | school_91 | 6 | |
| 95 | school_92 | 19 | |
| 96 | school_93 | 30 | |
| 97 | school_94 | 45 | |
| 98 | school_95 | 28 | |
| 99 | school_96 | 14 | |
| 100 | school_97 | 26 | |
| 101 | school_98 | 49 | |
| 102 | school_99 | 2 | |
+-----+-----------+-----+-----------------+
101 rows in set (0.00 sec)
#在django中查询数据库数据,返回的是集合
In [42]: sces=School.objects.all()
In [43]: sces
Out[43]: <QuerySet [<School: School object (1)>, <School: School object (3)>, <School: School object (4)>, <School: School object (5)>, <School: School object (6)>, <School: School object (7)>, <School: School object (8)>, <School: School object (9)>, <School: School object (10)>, <School: School object (11)>, <School: School object (12)>, <School: School object (13)>, <School: School object (14)>, <School: School object (15)>, <School: School object (16)>, <School: School object (17)>, <School: School object (18)>, <School: School object (19)>, <School: School object (20)>, <School: School object (21)>, '...(remaining elements truncated)...']>
In [44]:
#按条件查询
In [44]: School.objects.filter(name='school_1',age=14)
Out[44]: <QuerySet [<School: School object (4)>]>
In [45]: School.objects.filter(name='school_1',age=18)
Out[45]: <QuerySet []>
#不包含查询exclude()
#exclude相当于select * from table_name where not(name='xxx',age=10)
In [44]: School.objects.filter(name='school_1',age=14)
Out[44]: <QuerySet [<School: School object (4)>]>
In [45]: School.objects.filter(name='school_1',age=18)
Out[45]: <QuerySet []>
In [47]: sces=School.objects.exclude(name='school_1',age=14)
In [48]: len(sces)
Out[48]: 100
#get返回对象而不是集合,如果数据库中只有一条数据用get,不重复数据用get
In [49]: School.objects.get(name='school_1')
Out[49]: <School: School object (4)>
In [50]:
#获取主键 和获取id
In [50]: School.objects.get(pk=1)
Out[50]: <School: School object (1)>
In [51]: School.objects.get(id=1)
Out[51]: <School: School object (1)>
Q查询
In [62]: from django.db.models import Q
In [63]: School.objects.filter(Q(age=1)|Q(age=2))
Out[63]: <QuerySet [<School: School object (8)>, <School: School object (9)>, <School: School object (12)>, <School: School object (16)>, <School: School object (51)>, <School: School object (56)>, <School: School object (59)>, <School: School object (72)>, <School: School object (74)>, <School: School object (85)>, <School: School object (102)>]>
In [64]: School.objects.filter(Q(age=1)|Q(name='school_90'))
Out[64]: <QuerySet [<School: School object (51)>, <School: School object (74)>, <School: School object (93)>]>
In [65]:
级联查询
#一直filter级联下去
School.objects.all().filter(age__gt=10).filter(age__lt=20).exclude()
offset和limit
result_list[start:end] :从结果集的第start位置开始到底end结束的数据(不能够使用负数)
In [66]: all_sc=School.objects.all()
In [67]: all_sc[10:20]
#取最后一位,倒叙后取第一个
In [68]: all_sc.reverse()[0]
Out[68]: <School: School object (1)>