日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Django--filter()-字段查找(双下划线的使用详解)

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Django--filter()-字段查找(双下划线的使用详解) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Django--filter()-字段查找(雙下劃線的使用詳解)


?在了解django中的字段查找的同時,讓我們先熟悉一下比較符:

大于--gt-(greater than)  小于--lt-(less than)  等于--eq-(equal)  大于等于--gte  小于等于--lte

?

>>> from queryset_demo.models import Blog,Author,Entry >>> entry = Entry.objects.filter(pub_date__lte='2017-01-01') >>> entry <QuerySet [<Entry: python-2015>, <Entry: python-2014>, <Entry: python-4>]> >>> [d.id for d in entry] # 返回數據的id [5, 6, 7]

對應的sql語句為

mysql> select * from queryset_demo_entry-> ; +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | | 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 | | 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 | | 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 | | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ 7 rows in set (0.00 sec)mysql> select * from queryset_demo_entry where pub_date<2017-01-01; Empty set, 1 warning (0.00 sec)mysql> select * from queryset_demo_entry where pub_date<'2017-01-01'; +----+-------------+-----------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+-----------+------------+------------+------------+-------------+--------+---------+ | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | +----+-------------+-----------+------------+------------+------------+-------------+--------+---------+ 3 rows in set (0.00 sec) 對應的sql語句

對外鍵ForeignKey的字段查找,同時也是一個特殊的地方就是可以通過單下劃線

>>> entry <QuerySet [<Entry: python>, <Entry: python-4>]> >>> type(entry) <class 'django.db.models.query.QuerySet'> >>> [d.id for d in entry] [1, 7]

sql

mysql> select * from queryset_demo_entry-> ; +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | | 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 | | 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 | | 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 | | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ 7 rows in set (0.00 sec) View Code

?

精確查找exact

>>> entry=Entry.objects.get(body_text__exact="This is a demo") >>> entry.id 1>>> entry=Entry.objects.get(body_text="This is a demo") >>> entry.id 1

?

sql

mysql> select * from queryset_demo_entry-> ; +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | | 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 | | 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 | | 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 | | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ 7 rows in set (0.00 sec)mysql> select * from queryset_demo_entry where body_text ='This is a demo'; +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | +----+----------+----------------+------------+------------+------------+-------------+--------+---------+ 1 row in set (0.00 sec) View Code

?

?

跨表查詢

>>> Entry.objects.filter(blog__name='blog_3') <QuerySet [<Entry: python>, <Entry: python-4>]> >>> entry=Entry.objects.filter(blog__name='blog_3') >>> [d.id for d in entry] [1, 7]

sql

mysql> select * from queryset_demo_blog; +----+-----------------+------------------------------+ | id | name | tagline | +----+-----------------+------------------------------+ | 1 | change_new_name | All the latest Beatles news. | | 2 | create_test | This is the wayof create | | 3 | Cheddar Talk | One to Many test | | 4 | blog_3 | this is a test | +----+-----------------+------------------------------+ 4 rows in set (0.00 sec)mysql> select * from queryset_demo_entry-> ; +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | | 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 | | 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 | | 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 | | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ 7 rows in set (0.00 sec) View Code

?

反向查詢

通過blog表進行查詢

它也可以倒退。要引用“反向”關系,只需使用模型的小寫名稱即可。

這個例子檢索所有Blog具有至少一個對象Entry?,其headline包含'Lennon':

>>> from queryset_demo.models import * >>> blog = Blog.objects.filter(entry__body_text__contains='dj') >>> blog <QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: change_new_name>]> >>> [d.id for d in blog] [2, 3, 1] >>>

?

跨越多值關系

使用下劃線查找多值得關系

>>> blog = Blog.objects.filter(entry__body_text__contains='dj') >>> blog <QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: change_new_name>]> >>> [d.id for d in blog] [2, 3, 1] >>> blog = Blog.objects.filter(entry__body_text__contains='dj',entry__pub_date__year=2017) >>> [d.id for d in blog] [3, 1] >>> blog = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017) >>> blog_2 = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017) >>> blog_2 <QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]> >>> blog <QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]> >>>

英文:

  Suppose there is only one blog that had both entries containing?“Lennon”?and entries from 2008, but that none of the entries from 2008 contained?“Lennon”. The first query would not return any blogs, but the   second query would return that one blog.

  In the second example, the first filter restricts the queryset to all those blogs linked to entries with?“Lennon”?in the headline. The second filter restricts the set of blogs?further?to those that are also linked to entries that were published in 2008. The entries selected by the second filter may or may not be the same as the entries in the first filter. We are filtering the?Blog?items with each filter statement, not the?Entry?items.

理解:

?

數據庫進行數據的添加

mysql> mysql> select * from queryset_demo_entry; +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | id | headline | body_text | pub_date | mod_date | n_comments | n_pingbacks | rating | blog_id | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ | 1 | python | This is a demo | 2018-07-11 | 2018-07-14 | 10 | 29 | 23 | 4 | | 2 | python | django | 2018-07-12 | 2018-07-28 | 2 | 2 | 2 | 2 | | 3 | python-2017 | djagoddd | 2017-06-01 | 2017-08-14 | 55 | 676 | 88 | 3 | | 4 | python-2017 | django-ll | 2017-03-14 | 2017-07-14 | 22 | 33 | 44 | 1 | | 5 | python-2015 | ddd | 2015-07-14 | 2015-07-14 | 33 | 33 | 33 | 1 | | 6 | python-2014 | ddd | 2014-07-14 | 2014-07-14 | 22 | 33 | 33 | 3 | | 7 | python-4 | ddd | 2014-07-14 | 2018-07-04 | 66 | 66 | 66 | 4 | | 8 | demo | demo | 2017-06-16 | 2017-05-16 | 33 | 33 | 5 | 2 | | 9 | demo2 | zzddxx | 2017-02-16 | 2017-02-04 | 44 | 33 | 22 | 3 | +----+-------------+----------------+------------+------------+------------+-------------+--------+---------+ 9 rows in set (0.00 sec)mysql> select * from queryset_demo_blog; +----+-----------------+------------------------------+ | id | name | tagline | +----+-----------------+------------------------------+ | 1 | change_new_name | All the latest Beatles news. | | 2 | create_test | This is the wayof create | | 3 | Cheddar Talk | One to Many test | | 4 | blog_3 | this is a test | +----+-----------------+------------------------------+ 4 rows in set (0.01 sec)

?

?

?

?

shell進行測試

>>> blog = Blog.objects.filter(entry__body_text__contains='dj',entry__pub_date__year=2017) >>> blog <QuerySet [<Blog: Cheddar Talk>, <Blog: change_new_name>]> >>> >>> >>> blog_2 = Blog.objects.filter(entry__body_text__contains='dj').filter(entry__pub_date__year=2017) >>> blog_2 <QuerySet [<Blog: create_test>, <Blog: Cheddar Talk>, <Blog: Cheddar Talk>, <Blog: change_new_name>]> >>> [d.id for d in blog] [3, 1] >>> [d.id for d in blog_2] [2, 3, 3, 1]

?

轉載于:https://www.cnblogs.com/Echo-O/p/9311891.html

總結

以上是生活随笔為你收集整理的Django--filter()-字段查找(双下划线的使用详解)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。