Django ORM操作のマルチテーブル共同チェックの概要

django.shortcutsからimport render、HttpResponse

#ここでビューを作成します。


app01インポートモデルから

def query(request):

#####################オブジェクトベースのクエリ(サブクエリ)##################### ##########
フィールド別(公開)
#1 対多の本----------------->公開
#<--------- -------
#book_set.all()

#フィールドによるクエリの転送:

#この本の出版社のメールボックスをpythonでクエリする

#python = models.Book.objects.filter(title = "python")。first()
#print(python.publish.email)


#テーブル名_set.all()で小文字の逆クエリ

#Apple Pressから出版された本

#publish_obj = models.Publish.objects.filter(name = "sync果出版社").first()
#publish_obj.book_set.all()のobjの場合:
#print(obj.title)

#フィールド別(authors.all())
#多対多の本----------------------->著者
#<-------- --------
#book_set.all()



#python 作者の年齢を問い合わせます#python = models.Book.objects.filter(title = "python")。最初の()
#python.authors.allの作者の():
#print(author.name、author.age)

#alexによって発行された書籍のタイトルを照会します

#alex = models.Author.objects.filter(name = "alex")。first()
#alex.book_set.all()の本の場合:
#print(book.title)

#フィールドauthorAuthor
#多対多の著者-----------------------> authordetail
#<-------------- -
#テーブル名による作成者



#アレックスの電話番号を照会する#alex = models.Author.objects.filter(name = 'alex')。First()
#print(alex.authorDetail.telephone)


#山東省で著者の名前を照会する

#ad_list = models.AuthorDetail.objects.filter(addr = "shandong")
##
ad_listの広告:
#print(ad.author.name)

 

'' '
sqlに対応:

Book = title = "python"からpublish_idを選択し
、nid = 1


'' 'のPublishからメールを選択します

 


#####################クエリセットと__query(結合クエリ)##################に基づく##########

#前方クエリ:フィールドによる逆クエリ:小文字のテーブル名


#この本のpythonの発行元のメールボックスを照会します
#ret = models.Book.objects.filter(title = "python")。値( "publish__email")
#印刷(ret.query)

'' '
ブックからpublish.emailを選択します
左に参加しますbook.publish_id = publish.nid
パブリッシュしますwhere book.title = "python"
' ''

#Apple Press Publishing Book Name
#方法1:
ret1 = models.Publish.objects.filter(name = "Apple Press")。値( "book__title")
print( "111111111 ====>"、ret1。 query)#メソッド
2:
ret2 = models.Book.objects.filter(publish__name = "Apple Press")。values( "title")
print( "2222222222 ====>"、ret2.query)

#Query alex的手机号
#方法1:
ret = models.Author.objects.filter(name = "alex")。値( "authorDetail__telephone")

#方式2:
models.AuthorDetail.objects.filter(author__name = "alex")。values( "telephone")

#携帯電話番号が151で始まる著者が出版した本の名前と、その本に対応する出版社の名前を照会します

ret = models.Book.objects.filter(authors__authorDetail__telephone__startswith = "151")。values( 'title'、 "publish__name")
print(ret.query)


HttpResponse( "OK")を返す

おすすめ

転載: www.cnblogs.com/w770762632/p/12703731.html
おすすめ