Many-related operations of three ways of Django ORM framework (V)

In the previous blog has been told to use an ORM-many relationship table, now summarize:

1.ORM automatically help us create the third table

2. Manually create a third table, the third table using ForeignKey pointing to the other two tables associate

3. Manually create a third table, use ORM and help us create the same (this method being first does not go into details)

 

 

The blog focus to talk about the second method: manually create the third table, and the third table using ForeignKey pointing to two other tables associate

And the author of the book publishing an example for the child. Author can write more than one book, a book can have multiple authors, the relationship between the book and the author's relationship is many-to

1. Create two respective table (Table author and book tables)

class usertest (models.Model):
     "" " 
    On the table 
    fields: id, name of the author 
    " "" 
    the above mentioned id = models.AutoField (primary_key = True) 
    name = models.CharField (max_length = 20 ) 


class BookTest (models.Model):
     "" " 
    books table 
    fields: id, title of the book 
    " "" 
    the above mentioned id = models.AutoField (primary_key = True) 
    name = models.CharField (max_length = 20)

At present two tables belong to has nothing to do, the next step is to create a third table, using a third table to make the two tables associate, essentially using the third table books and records of the table id table id respectively as a foreign key

class UserAndBook (models.Model):
     "" " 
    third table 
    fields: user id field is a record of the table, book id field is the record book table 
    " "" 
    id = models.AutoField (primary_key = True) 
    the User = Models .ForeignKey (to = " usertest " ) 
    Book = models.ForeignKey (to = " BookTest " )

Id = 1 query of how much of a book published this

(1) first query the third table all the information in the user = 1, which is the author id = 1.

get_all_book_id=models.UserAndBook.objects.filter(user=1).values_list("book")

Sentence retrieves all user information table UserAndBook = 1, then only queries book data fields, back through the list, the list of objects returned, each element is a tuple.

(2) At this time got id = id of all the books under the name of 1, since (1) taken out of the get_all_book variable is a list, and each element is a tuple, so we need to talk about each book id are taken out, the use of lambda expressions or the other can

ret=[i[0] for i in get_all_book_id]

(3) to get the id query table books books

show_all_books=models.BookTest.objects.filter(id__in=ret)

At this point it is to get all the books in the instance of id = 1.

 

======================================================================

A third method of temporarily updating, and so have come up time

pass

 

======================================================================

There may be students ask, do not you use to automatically create tables, why manual creation, and here I explain, first of all we can see, the third table is automatically created which records only the other two tables corresponding to the id, no other field, but if because of business needs, you need to add a few fields in the third table, then automatically create a table is not too meet our needs, so we had to manually create a table on a role.

Guess you like

Origin www.cnblogs.com/fjiqiang/p/10936339.html