一、创建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 = 20In [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]: 20In [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 = 100In [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]: 100In [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 randomIn [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 QIn [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)>

win10+python开发django项目day03相关推荐

  1. 在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片

    在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片 1.首先springboot项目在Window和Li ...

  2. 使用Python开发DeFi项目

    原文链接:https://blog.chain.link/develop-python-defi-project/ 原文标题:Develop a DeFi Project Using Python 原 ...

  3. Python开发-Django 开源项目

    Django 开源项目 了解 Django 项目本身的开发进程以及你如何为 Django 做贡献: 一.社区: 如何参与其中 为 Django 做贡献 Django 是一个以志愿者为生的社区.随着它的 ...

  4. python创建django项目_搭建Python-Django环境,创建第一个Django项目

    曾想学爬虫,没想到误入python web班,在美女老师shirely的指导下,搭建了好Django环境,试着开始做第一个web项目 一.Python环境安装 1.Python2.7的下载 从Pyth ...

  5. Python开发-Django快速入门

    如果你是一位python开发初学者,而且你对Django一无所知,那下面介绍的内容将会是你所需要知道的关于 Django 的知识. 一.快速入门 你是刚学 Python.Django 或是初学编程吗? ...

  6. Python开发-Django安全

    安全 在 Web 应用的发展中,安全是最重要主题,Django 提供了多种保护手段和机制. 一.安全概览 Django 的安全性 Django 安全特性的概述.包含保障那些用 Django 建立的网站 ...

  7. python开发web项目_Django2:Web项目开发入门笔记(20)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 这一篇教程,我们一起来了解如何在CentOS系统中将Django2的Web项目部署到Nginx服务器. CentOS系统虽然和Ubuntu系统都是Linu ...

  8. python编程django项目django.template.exceptions.TemplateDoesNotExist: registration/login.html解决方法

    文章目录 遇到的问题 解决方法 参考 遇到的问题 做<python编程:从入门到实践>的web项目:学习笔记. 在19章,遇到问题:添加user应用程序,编写完login.html,进行浏 ...

  9. python编程django项目中ModuleNotFoundError: No module named ‘django.core.urlresolvers‘解决方法

    文章目录 遇到的问题 解决方法 参考 遇到的问题 在做<python编程-从入门到实践>书中的项目的时候,遇到No module named 'django.core.urlresolve ...

最新文章

  1. python箱线图_Python 箱线图 plt.boxplot() 参数详解
  2. oracle日志备份少数据库,oracle 账号锁定日志Oracle数据库全量备份恢复和部分备份恢复...
  3. OpenCASCADE:使用扩展数据交换 XDE之读写 STEP 或 IGES
  4. CLR via C# (二)
  5. IE浏览器高级设置如何还原
  6. 7-88 二叉搜索树的结构 (30 分)
  7. teleport 组件的作用_对于组件的可重用性,大佬给出来6个级别的见解,一起过目一下
  8. 41.MySQL 主从复制, 双主热备
  9. oracle空值问题
  10. jsp查询不到mysql内容_java查询到mysql数据库的数据,jspbean查询不到
  11. 华为手机解锁码计算工具_手机计算器全线阵亡?小编实测苹果、华为、三星……...
  12. 免费下载网易云音乐付费歌曲及下架歌曲
  13. jquery实现向服务器发送get请求下载excel文件
  14. 《编程的原则:改善代码质量的101个方法》读书笔记
  15. 关于打开网页FLASH显示叉叉问题
  16. 验证英文日期格式的正则表达式
  17. python怎么把照片转成卡通_如何把照片变成手绘动漫化?
  18. 浅谈用友NC产品单点登录机制
  19. source insight 多文件并行显示
  20. Java三种设计模式

热门文章

  1. 怎么在手机上取消双重认证_用手机在淘宝上怎么开网店?流程步骤详解
  2. js json数据去重。json数据如何将相邻的一条数据的重复数据删掉,通过key判断值相等的
  3. 2022Android笔试真题,20道高频面试题(含答案)
  4. JAVA创建一个Box类(长方体),在Box类中定义三个变量,分别表示长方体的长(length)、宽(width)和高(heigth)
  5. 哪些著名软件是用C、C++编写的?
  6. 修改Thinkphp-cmf上传视频大小限制
  7. sm羞耻任务_羞耻驱动的发展
  8. OpenCV读取文件夹下的图片生成视频(mp4格式)
  9. PySlowFast 平台的使用及解析——以X3D为例
  10. SSH 通过跳板机连接远程主机