数据库正向与反向迁移

正向迁移(将django中的类导入到MySQL中变成表)
	python3 manage.py makemigrations
    python3 manage.py migrate
反向迁移
	inspectdb

django脚本环境

django默认情况下是不允许单独使用某个功能部分(models.py)


1.要么自己新建一个py文件
2.要么使用自带的tests文件

import os
if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day47.settings")
    import django
    django.setup()
    # 测试代码

关键字

# 增     1.create()
    # models.Books.objects.create(title='三国演义',price=456.23)
    # models.Books.objects.create(title='水浒传',price=876.45)
    # models.Books.objects.create(title='聊斋志异',price=123.69)
    # models.Books.objects.create(title='草堂笔记',price=456.96)

    # 查     2.all()
    # res = models.Books.objects.all()
    # print(res)  # QuerySet对象
    # print(res.query)  # 只要是QuerySet对象就可以点query查看内部SQL语句

    # 查     3.filter()
    # res1 = models.Books.objects.filter()  # pk特指当前表的主键字段
    # print(res1)  # QuerySet对象
    # print(res1.query)
    # print(res1.first())  # 获取列表中第一个数据对象
    # print(res1.last())  # 获取列表中最后一个数据对象

    # 4.values与5.values_list
    # res2 = models.Books.objects.values('title','price')
    # print(res2)  # QuerySet对象   可以看成列表套字典
    # print(res2.query)

    # res3 = models.Books.objects.values_list('title','price')
    # print(res3)  # QuerySet对象     可以看成列表套元祖
    # print(res3.query)

    # 6.查 get()     不推荐使用
    # res4 = models.Books.objects.get(pk=1)
    # print(res4)  # 数据对象
    # res5 = models.Books.objects.get(pk=100)
    # print(res5)  # 数据对象
    # res6 = models.Books.objects.filter(pk=100)
    # print(res6)  # 数据对象

    # 7.取反  exclude()
    # res7 = models.Books.objects.exclude(pk=1)
    # print(res7)  # QuerySet
    # print(res7.query)


    # 8.排序   order_by()      默认是升序(asc)    字段前面加负号降序(desc)
    # res8 = models.Books.objects.order_by('price')
    # res8 = models.Books.objects.order_by('-price')
    # select * from books order by price desc,price asc;
    # res8 = models.Books.objects.order_by('-price','price')
    # print(res8)  # QuerySet对象
    # print(res8.query)


    # 9.反转  reverse()       必须先有顺序才可以反转
    # res9 = models.Books.objects.all()
    # res9 = models.Books.objects.order_by('price').reverse()
    # print(res9)

    # 10.去重 distinct()
    # res10 = models.Books.objects.all().distinct()
    # res10 = models.Books.objects.values('title','price').distinct()
    # print(res10)

    # 11.计数 count()
    # res11 = models.Books.objects.count()
    # print(res11)  # 6

    # 12.判断是否有数据   exists()
    # res12 = models.Books.objects.filter(pk=999).exists()
    # print(res12)  # False

    # 13.update()
    # 14.delete()

神奇的双下划线

# 查询价格大于200的书籍
    # res = models.Books.objects.filter(price__gt=200)
    # print(res)
    # res1 = models.Books.objects.filter(price__lt=200)
    # print(res1)
    # res2 = models.Books.objects.filter(price__gte=456.23)
    # print(res2)
    # res3 = models.Books.objects.filter(price__lte=456.23)
    # print(res3)

    # 成员运算
    # res4 = models.Books.objects.filter(price__in=(456.23,111))
    # print(res4)
    # 范围查询
    # res5 = models.Books.objects.filter(price__range=(100,456.23))
    # print(res5)
    # 模糊查询
    # 查询书籍名称中含有字母a的书
    # res6 = models.Books.objects.filter(title__contains='a')
    # print(res6)  # 区分
    # res7 = models.Books.objects.filter(title__icontains='a')
    # print(res7)  # 忽略

    # 日期相关
    # 查看出版月份是五月的书
    # res8 = models.Books.objects.filter(publish_time__month=5)
    # print(res8)
    # print(res8.query)
    # res9 = models.Books.objects.filter(publish_time__year=2021)
    # print(res9)

外键字段增删改查

# 增
    # models.Book.objects.create(title='三国演义',price=345.43,publish_id=1)
    # models.Book.objects.create(title='红楼梦',price=678.31,publish_id=2)

    # publish_obj = models.Publish.objects.filter(pk=2).first()
    # models.Book.objects.create(title='三国演义',price=345.43,publish=publish_obj)
    # models.Book.objects.create(title='七龙珠',price=908.43,publish=publish_obj)

    # 改
    # models.Book.objects.filter(pk=2).update(publish_id=1)
    # models.Book.objects.filter(pk=2).update(publish=publish_obj)

    # 删 级联更新级联删除
    # models.Publish.objects.filter(pk=1).delete()


    # 多对多
    book_obj = models.Book.objects.filter(pk=3).first()
    # 绑定关系
    # book_obj.authors.add(1)  # 去书与作者的关系表中绑定关系
    # book_obj.authors.add(1,2)  # 去书与作者的关系表中绑定关系
    # book_obj.authors.add(author_obj1)
    # book_obj.authors.add(author_obj1,author_obj2)

    # 修改关系
    # book_obj.authors.set([1,])
    # book_obj.authors.set([1,2])
    # book_obj.authors.set([author_obj,])
    # book_obj.authors.set([author_obj1,author_obj2])

    # 移除关系
    # book_obj.authors.remove(1)
    # book_obj.authors.remove(1,2)
    # book_obj.authors.remove(author_obj,)
    # book_obj.authors.remove(author_obj1,author_obj2)

    # 清空关系
    book_obj.authors.clear()
posted on 2021-05-25 14:25  lzl_121  阅读(49)  评论(0编辑  收藏  举报