ORM补充

Person.objects.dates('birth', 'month', order='DESC')  // 按日期归档
Person.objects.datetimes('birth', 'month', order='DESC')

 多对多反查询

related_name='dishes'

 

    # values 取具体的数据 -> 对象列表 元素 {} 字段:值
    # 没有指定参数,获取所有的字段数据
    # 指定参数,获取指定的字段数据
    obj = Person.objects.values()
    obj = Person.objects.values('id', 'name')
    # values_list 取具体的数据 -> 对象列表 元素 () 值
    # 没有指定参数,获取所有的字段数据
    # 指定参数,获取指定的字段数据
    obj = Person.objects.values_list()
    obj = Person.objects.values_list('id', 'name')

 

class Book(models.Model):
    title = models.CharField(max_length=30)
    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)

Publish.objects.get(name='清华大学出版社').book_set.all() // 查询清华大学出版社出版的所有书籍


class Book(models.Model):
    title = models.CharField(max_length=30)
    publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE, related_name='books')

Publish.objects.get(name='清华大学出版社').books.all()

Publish.objects.filter(id=Book.objects.get(title='Python基础').publish_id)
Publish.objects.filter(books__title='Python基础') book_set和books是RelatedManager(关联管理器)

 

Person.objects.filter(id__in=[1,3])
Person.objects.filter(id__gte=2, id__lte=3)
Person.objects.filter(id__range=[2,3])
Person.objects.filter(name__contains='ar')
Person.objects.filter(name__icontains='ar') // icontains忽略大小写

Person.objects.filter(birth__year="2019")
Person.objects.filter(birth__month="08")

 

# 单表正向查询
Book.objects.get(id=2).publish_id
Book.objects.get(id=2).publish
Book.objects.get(id=2).publish.id
Book.objects.get(id=2).publish.name

# 跨表正向查询使用"__"
Book.objects.filter(publish_id=Publish.objects.get(name='清华大学出版社').id)
Book.objects.filter(publish__name='清华大学出版社')
Book.objects.all().values_list('title', 'publish__name')

# 跨表反向查询使用"多表名__set"或在多表类中设置related_name
Publish.objects.get(name='清华大学出版社').book_set.all()
Publish.objects.get(name='清华大学出版社').books  # related_name='books'
Publish.objects.filter(id=Book.objects.get(title='Python基础').publish_id)
Publish.objects.filter(books__title='Python基础')  # related_name='books'
Publish.objects.filter(book__title='Python基础')
Author.objects.get(id=2).books.all()  # related_name='books'

# 补充
# 在Book类publish字段设置related_query_name='ooxx',仅字段查询时可以使用
Publish.objects.filter(ooxx__title='Python基础')  # related_query_name='ooxx'

 

type(Author.objects.get(id=2).books) ManyRelatedManager(关联管理器)

# 多对多增删改查
Author.objects.get(id=2).books.set([3,4,5,6])  # 修改
Author.objects.get(id=2).books.set(6)  # 错误,set参数必须是列表(list)或元组(tuple)
Author.objects.get(id=2).books.set(Book.objects.all())  # 参数直接是对象

Author.objects.get(id=2).books.add(3,4,5,6)  # 追加,add参数必须是多个参数
Author.objects.get(id=2).books.add(*[3,4,5,6])
Author.objects.get(id=2).books.add(*Book.objects.all())

Author.objects.get(id=2).books.remove(5,6)  # 删除
Author.objects.get(id=2).books.remove(*[3,4,5,6])
Author.objects.get(id=2).books.remove(*Book.objects.all())

Author.objects.get(id=2).books.clear()  # 清除
Author.objects.get(id=2).books.create(title="SQL高级v2.0", publish_id='1') # 创建

obj = Author.objects.get(id=2).books.all()  # 查询

obj = Book.objects.get(id=5).author_set.all()
# models.py里Author类设置books = models.ManyToManyField(to='Book', related_name='authors')
obj = Book.objects.get(id=5).authors.all()

  

posted @ 2019-09-06 12:37  arley520  阅读(92)  评论(0编辑  收藏  举报