First, the relationship between the table and the table
One to One: (OneToOneField) relationship field no matter where Zhang Jian table can be, but it is recommended to build a relatively high frequency in the query that table
【publish = models.ForeignKey(to='Publish')】
Many: (ForeignKey) built in many-to-many fields that party
Many to many: (ManyToManyField) many-field no matter where Zhang Jian relationship table can be, but it is recommended to build a relatively high frequency in the query that table
ps: how to judge in the end what is the relationship between the table and the table
Empathy: A can have a plurality of B
B can have a plurality of A
Delete additions and changes: can be a number, it can be an object
add () # add
set () # modifications
remove () # iterables not received, the object can be * querySet
clear () # Empty without mass participation
Set delete cascading delete:
publish = models.ForeignKey(to='Publish', on_delete=models.CASCADE)
= models.ForeignKey publish (to = 'Publish', on_delete = models.SET_NULL, null = True) # cancel cascading deletes sure to set null = True
CASCADE: Set cascading deletes, which removed the Press, the publishers of the corresponding book will be a delete
SET_NULL: Cancel cascading deletes
Forward and reverse concept
models:
class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8, decimal_places=2) publist_date = models.DateField(auto_now_add=True) # publish = models.ForeignKey(to='Publish', to_field='id', on_delete=models.CASCADE,) publish = models.ForeignKey(to='Publish', on_delete=models.SET_NULL, null=True) authors = models.ManyToManyField(to='Author') # 虚拟字段 class Publish(models.Model): name = models.CharField(max_length=32) addr = models.CharField(max_length=32) email = models.EmailField() # 对应就是varchar类型 class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() authordetail = models.OneToOneField(to='AuthorDetail') class AuthorDetail(models.Model): phone = models.CharField(max_length=32) addr = models.CharField(max_length=32)
Forward queries by field, press the reverse lookup table name in lowercase ...
one
forward: author --- author related fields in the table ---> authordetail by field
Reverse: authordetail --- author related fields table ---> author lowercase table names by
the query jason phone number of positive inquiry
inquiry address is: name of the author of Shandong reverse lookup
many
positive: book --- table associated field in book --- > publish by field
reverse: publish --- related field book list ---> book more books by table name in lowercase _set.all () as a publishing house corresponds to
Many-
Forward: book --- book table in the associated field ---> author by field
reverse: author --- related field book list ---> book by table name in lowercase _set.all ( ) because of a corresponding plurality of books
queries Oriental Press is publishing books published many fields reverse lookup
publish_obj = models.Publish.objects.filter (name = 'Oriental Press') .first ()
Print (publish_obj.book_set) # app01.Book.None
Print (publish_obj.book_set.all ())
all the books-many fields of inquiry jason wrote reverse lookup
author_obj = models.Author.objects.filter (name = 'Jason'). First ()
Print (author_obj.book_set) # app01.Book.None
Print (author_obj.book_set.all ())
query of the telephone number is one author name field 110 of reverse lookup
authordetail_obj = models .AuthorDetail.objects.filter (Phone = 110) .first ()
Print (authordetail_obj.author.name)