- Django 2 by Example
- Antonio Melé
- 105字
- 2021-06-25 21:19:01
Using the filter() method
To filter a QuerySet, you can use the filter() method of the manager. For example, we can retrieve all posts published in the year 2017 using the following QuerySet:
Post.objects.filter(publish__year=2017)
You can also filter by multiple fields. For example, we can retrieve all posts published in 2017 by the author with the username admin:
Post.objects.filter(publish__year=2017, author__username='admin')
This equates to building the same QuerySet chaining multiple filters:
Post.objects.filter(publish__year=2017) \
.filter(author__username='admin')
Queries with field lookup methods are built using two underscores, for example, publish__year, but the same notation is also used for accessing fields of related models, such as author__username.
推薦閱讀
- 黑客攻防實戰技術完全手冊:掃描、嗅探、入侵與防御
- Mastering Node.js(Second Edition)
- 物聯網智慧安監技術
- 智能網聯汽車V2X與智能網聯設施I2X
- Truffle Quick Start Guide
- TD-LTE無線網絡規劃與設計
- Getting Started with Memcached
- 從實踐中學習手機抓包與數據分析
- Practical Web Penetration Testing
- 現代通信系統(第5版)
- 工業以太網技術:AFDX/TTE網絡原理、接口、互連與安全
- Hands-On Reactive Programming in Spring 5
- 想象的互動:網絡人際傳播中的印象形成
- Architecting Data:Intensive Applications
- Microsoft System Center 2012 Configuration Manager:Administration Cookbook