一、模型层
'''django自带的sqlite数据库功能非常的少 并且对日期格式不兼容'''1.MySQL配置2.数据库迁移命令3.ORM查询关键字
二、测试环境
1.python自带的console2.py文件形式(参考manage.py前四行)import osif __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "d19_dj6.settings") import django django.setup() # 在该代码的下方才可以正常测试django文件
三、查询关键字
# 查 关键词: 1、all() # 输出结果可以认为是:列表套对象 2、filter() 3、values() # 可以查询指定字段,输出结果可以认为是:列表套字段(列表套字典) 4、values_list() 5、first()与last() 6、get() # 不推荐使用,django会报错 7、exclude() # 取反 QuerySet 列表套数据对象 8、order_by() # 排序,降序 9、reverse() # 翻转 10、distinct() # 去重 11、count() # 计数 12、exists() # 判断是否存在 13、create()、update()、delete()、对象.save() # 1.all() # res = models.Books.objects.all() # QuerySet # print(res) # print(res.first()) # print(res.last()) # print(res[1]) '''queryset支持正数的索引取值''' # print(res) # 2.filter() # res = models.Books.objects.filter(id=1) QuerySet '''pk能够自动定位当前表的主键字段 避免了你自己去频繁查看''' # res = models.Books.objects.filter(pk=2) QuerySet # print(res) '''filter括号内可以存放多个条件默认是and关系''' # res = models.Books.objects.filter(pk=2,title='jpm') # print(res) # 3.values() '''values可以指定查询的字段 结果QuerySet 列表套字段''' # res = models.Books.objects.values('title','price') # 4.values_list() '''values_list也可以指定查询的字段 结果QuerySet 列表套元组''' # res = models.Books.objects.values_list('title', 'price') # print(res) # 5.first()与last() 分别获取queryset第一个和最后一个数据对象 # res = models.Books.objects.all().filter().values('title','price') # print(res) '''只要是queryset对象 就可以无限制的调用queryset对象的方法''' # 6.get() 不推荐使用 # res = models.Books.objects.get(pk=1) '''get方法可以直接获取到数据对象''' # print(res,res.pk,res.title,res.price,res.publish_time) '''但是该方法当查询条件不存在的时候会直接报错 没有filter好用!!!''' # res = models.Books.objects.get(pk=100) # res1 = models.Books.objects.filter(pk=100) # print(res1) # 7.exclude() 取反 QuerySet 列表套数据对象 # res = models.Books.objects.exclude(pk=1) # print(res) # 8.order_by() 排序 QuerySet 列表套数据对象 # res = models.Books.objects.order_by('price') # print(res) # 9.reverse() 翻转(必须提前有顺序才可以) # res = models.Books.objects.all() # print(res) # print(res.order_by('price')) # print(res.order_by('price').reverse()) # 10.distinct() 去重 '''尤其要注意主键字段''' # res = models.Books.objects.all().values('title','price') # print(res) # print(res.distinct()) # 11.count() 计数 # res = models.Books.objects.all().count() # res1 = models.Books.objects.count() # print(res, res1) # 4 4 # 12.exists() 判断结果集是否有数据(不需要使用) # res = models.Books.objects.all().exists() # res1 = models.Books.objects.filter(id=100).exists() # print(res,res1) # True False # 13.create()、update()、delete()、对象.save()
四、如何查看SQL语句
1.如果当前对象是queryset那么可以直接点query查找该对象内部SQL语句2.配置文件(只要执行orm语句都会自动打印SQL语句) LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', }, }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'propagate': True, 'level':'DEBUG', }, } }
五、filter的神奇的双下划线查询
# 神奇双下划綫查询 1、字段名__gt= # 大于 greater than 2、字段名__gte= # 大于等于 gt or equal 3、__lt= # 小于 less than 4、__lte= # 小于等于 5、__in=[a,b,c] # 查找列表内的 6、__range=(a,b) # 范围查找,a到b之间 7、__contains= # 查找字段包含某个字符的,区分大小写 8、__icontains= # 查找字段包含某个字符的,忽略大小写 ignore 9、__startswith='xx' # 查找字段是以xx开头 10、__endswith= 11、__year=2021 # 查找date类型是2021的数据 12、__month= # 1.查询价格大于800的书籍 # res = models.Books.objects.filter(price__gt=800) # print(res) # 2.查询价格小于800的书籍 # res = models.Books.objects.filter(price__lt=800) # print(res) # 3.查询价格大于等于800的书籍 # res = models.Books.objects.filter(price__gte=800) # print(res) # 4.查询价格大于等于800的书籍 # res = models.Books.objects.filter(price__lte=800) # print(res) # 5.查询价格是989.45 或者278.89 或者888.21 # res = models.Books.objects.filter(price__in=["888.21","278.89","989.45"]) # print(res) # 6.查询加个在200到800之间的书籍 # res = models.Books.objects.filter(price__range=(200,800)) # print(res) # 7.查询书籍名称中包含字母p的书(区分大小写) # res = models.Books.objects.filter(title__contains='p') # print(res) # res = models.Books.objects.filter(title__icontains='p') # print(res) # 8.查询书籍是否以...开头 ...结尾 # res = models.Books.objects.filter(title__startswith=) # res1 = models.Books.objects.filter(title__endswith=) # 9.查询出版日期是2021的书(常用) res = models.Books.objects.filter(publish_time__year=2021) res1 = models.Books.objects.filter(publish_time__month=11) print(res,res1)
六、图书管理系统表设计
class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8, decimal_places=2) publish_time = models.DateField(auto_now_add=True) publish = models.ForeignKey(to='Publish') authors = models.ManyToManyField(to='Author')class Publish(models.Model): name = models.CharField(max_length=32) addr = models.CharField(max_length=64)class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() author_detail = models.OneToOneField(to='AuthorDetail')class AuthorDetail(models.Model): phone = models.BigIntegerField() addr = models.CharField(max_length=32)