Orm layer model and query

Orm layer model and query

A single-table query

1) preparatory work

 

Create a new database (on the line with navicate)

 

  • Modify the configuration settings

 

 

 

  • Add code

 

 

 

  • Create a table in the class began to file models.py

. 1  class Movie (models.Model):
 2      title = models.CharField (MAX_LENGTH = 32 )
 . 3      . Price = models.DecimalField (= max_digits. 8, decimal_places = 2 )
 . 4      publish_time models.DateField = ()    # represents the time of addition is month day 
5      '' ' 
6      supplement: DateField has two parameters, one is auto_now: expressed every time to modify the data will be saved with the new,
 7      and the other is auto_now_add, it represents only add time to start creating and behind will not change
 . 8      '' ' 
. 9      # publish_time = models.DateTimeField () represents the time when the date is added in minutes and seconds (with time)

 

  • migration database migration command execution by python mange.py makemigrations and python mange.py

 

 

  • How to set up a test py a separate file in Django

 1 # 将manage.py文件中的前四行代码(添加环境变量用的)拷贝到test.py(任意一个test.py文件都可以),然后再在test.py文件中写上下面两句代码即可
 2 import django、
 3 django.setup()
 4  5 from app01 import models
 6 models.Movie.objects.all
 7  8 # 这样在test文件中直接运行py文件就可以执行并看到效果
 9 10 11 # 整体设置代码如下
12 import os
13 if __name__ == "__main__":   os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day53.settings")
14     import django
15     django.setup()
16 17     from app01 import models
18     models.Movie.objects.all()

 

2)单表查询必知必会16条

        1.create()
        2.all()
        3.filter()
        4.update()
        5.delete()
        6.first()
        7.last()
        8.get()
        9.values()
        10.values_list()
        11.order_by()
        12.count()
        13.exclude()
        14.exists()
        15.reverse()
        16.distinct()            

 

 

1.create()

 1  # 1. create() :给数据库表添加内容,注意使用create是有返回值的,返回值就是当前被创建的数据的对象本身
 2     # 由于之前设置的时候没有给默认时间,这里可以手动填写时间
 3     models.Movie.objects.create(title='西游记', price=666.66, publish_time='2018-1-1')
 4     res = models.Movie.objects.create(title='红楼梦', price=555.55, publish_time='2018-1-1')
 5     print(res)
 6     # 也可以进行修改让其自动添加时间(现在是为了方便)
 7     from datetime import date
 8     ctime = date.today()
 9     models.Movie.objects.create(title='水浒传', price=888.88, publish_time=ctime)
10     models.Movie.objects.create(title='三国演义', price=999.99, publish_time=ctime)
11
creat()

2.all()

1  # 2.all()查询数据库所有内容对象返回的是一个queryset
2     # res = models.Movie.objects.all()
3     # print(res)
all()

3.filter()

 1 # 3.filter() 指定条件筛选,返回的是queryset对象 <QuerySet [<Movie: Movie object>]>,若不指定条件则查询所有
 2     res = models.Movie.objects.filter(id=1)
 3     print(res)
 4     # pk表示主键
 5     res = models.Movie.objects.filter(pk=1)
 6     print(res)
 7     # 括号内可以传多个条件,条件之间是and关系
 8     res = models.Movie.objects.filter(pk=1, title='水浒传')
 9     print(res)
10     # 只要是queryset对象  就可以通过 点query 的方式查看到获取到当前对象内部sql语句
11     print(res.query)  # SELECT `app01_movie`.`id`, `app01_movie`.`title`, `app01_movie`.`price`, `app01_movie`.`publish_time` FROM `app01_movie` WHERE (`app01_movie`.`id` = 1 AND `app01_movie`.`title` = 水浒传)
filter()

4.get()

1  # 4.get() 直接获取条件查询得到的数据的对象本身(和create创建时返回的是一样的)  不推荐使用  当查询条件不存在的时候直接报错
2     res = models.Movie.objects.get(pk=1)
3     print(res)
4     res = models.Movie.objects.get(pk=5)
5     print(res)  # 报错信息:app01.models.DoesNotExist: Movie matching query does not exist.
6     res = models.Movie.objects.filter(pk=5)  # 而使用filter查询找不到会返回一个空列表
7     print(res)
get()

5.values() 

1 # 5.values()  查询指定字段下的所有值,可以多个字段一起查,结果是列表套字典的形式
2     res = models.Movie.objects.values('title', 'price')
3     #查询结果:<QuerySet [{'title': '西游记', 'price': Decimal('666.66')}, {'title': '水浒传', 'price': Decimal('888.88')}, {'title': '三国演义', 'price': Decimal('999.99')}, {'title': '西游记', 'price': Decimal('666.66')}]>
4     print(res)
5     print(res.query)
6     # SELECT `app01_movie`.`title`, `app01_movie`.`price` FROM `app01_movie`
values()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiangxianseng/p/12169377.html