Cross-table query

Cross-table query

A, ORM cross-table query

class Book(models.Model):

title = models.CharField(max_length=32)

publish = models.ForeignKey(to="Publish",to_field="id",on_delete=models.CASCADE)

authors = models.ManyToManyField(to = "Author",related_name='bookList')



class Publish(models.Model):

name = models.CharField(max_length=32)



class Author(models.Model):

name = models.CharField(max_length=32)

ad = models.OneToOneField("AuthorDetail",on_delete=models.CASCADE)



class AuthorDetail(models.Model):

telephone = models.BigIntegerField()

  

1, based on an object query (sql: sub-query) :

Many, (Publish - Book)

Forward query by field: 

Query python name of the book publishing house where 

book_obj = Book.objects.filter (title = "python") First (). 

Print (book_obj.publish.name) 

reverse lookup, according to show lowercase _set: 

People's Publishing House published the names of all books 

publish_obj = Publish.objects.filter (name = "People's Publishing House") .first () 

Print (publish_obj.book_set.all ()) 

for obj in publish_obj.book_set. All (): 

Print (obj.title) displayed one title

  

 

Many to many,

Forward query by field: 

Python book all authors' names 

book_obj = Book.objects.filter (title = "Python") First (). 

Book_obj.authors.all () 

reverse lookup, according to show lowercase _set: 

alex all the books ever published name 

alex = Author.objects.filter (name = "alex ") first (). 

method one: alex.book_set.all () 

method II (which is set related_name = 'bookList' method): alex .bookList.all ()

  

One on one,

Forward query by field: 

Query phone number alex 

alex = Author.objects.filter (name = "alex") First (). 

Alex.ad.telephone 

reverse lookup, according to show lowercase: 

beginning with 151 phone numbers the author's name 

AD = AuthorDetail.objects.get (telephone__startswith = "151") 

ad.authour.name

  

2, based on Queryset and __ (sql: join statement):

Forward query by field 

reverse lookup, according to show lowercase 


-to-many, (Publish - Book) 

forward query by field: 

Query python book publishing house where the name 

Book.objects.filter (title = "python ") .values (" publish__name ") 

for obj in Book.objects.filter (title =" Python "): 

the TEMP = {} 

the TEMP [" publish__name "] = obj.publish.name 


reverse lookup, according to show lowercase: 

people Press published the names of all the books 

Publish.objects.filter (name = "People's Publishing House") .values ( "book__title") 

 

 

many to many, 

Python book all authors' names 

Book.objects.filter (title = . "Python") values ( "authors__name") 

 

alex all the books ever published names 

. Author.objects.filter (name = "alex" ) values ( "book__title") 
one on one, 
forward query by field: 

query alex phone number 

Author.objects.filter (name = "alex") . values ( "ad__telephone") 

 

to 151 at the beginning of the phone number of the author's name

AuthorDetail.objects.filter(telephone__startswith="151").values("author__name")  

 

Third, expand

eg1: 
Query python book publishing house where the name 

Book.objects.filter (title = "python") values ( "publish__name"). 

Publish.objects.filter (book__title = "python") values ( "name"). 

eg2: 
the author's name phone number beginning with 151 

. AuthorDetail.objects.filter (telephone__startswith = "151") values ( "author__name") 

Book.objects.filter (authors__ad__telephone__startswith = "151") values ( "title",. "publish__name")

  

 

Guess you like

Origin www.cnblogs.com/mainstream/p/11113410.html