官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 屏山县| 金山区| 鲁甸县| 诸城市| 阳春市| 金堂县| 都江堰市| 来凤县| 老河口市| 花垣县| 都匀市| 黎平县| 九江县| 修文县| 平和县| 赤壁市| 永清县| 桃源县| 普格县| 龙游县| 得荣县| 山西省| 金坛市| 息烽县| 太白县| 松桃| 阳东县| 肃南| 封开县| 楚雄市| 民丰县| 油尖旺区| 阿拉尔市| 西丰县| 济宁市| 文昌市| 定安县| 吉水县| 大庆市| 淮安市| 利辛县|