a foreign key django
Read this article, I feel very good example that
I define two models, one is the author, is a published author of books, regarded as the type of many.
class Person(models.Model); name = models.CharField('作者姓名', max_length=10) age = models.IntegerField('作者年龄')
class Book(models.Model): person = models.ForeignKey(Person, related_name='person_book') title = models.CharField('书籍名称', max_length=10) pubtime = models.DateField('出版时间')
If we want to query what one author has published books, then how do we want it?
The first query to the author
person = Person.objects.fiter(你的条件)
返回一个person对象
The next person to query all the books associated objects
book = person.book_set.all()
django default value for each object has a main table is a foreign key attribute can query information about all belonging to the master table through which the sub-table.
The default is the name of the property sub-table name in lowercase plus _set () to represent the default returns a querydict object, you can continue to query and other operations according to the situation.
If you feel that the above definition is too much trouble, you can also define a foreign key of the main table when a foreign key to the definition of a good name. Use ** related_name **
such as:
person = models.ForeignKey(Person, related_name='person_set')
那么上面的话:book = person.book_set.all()
也可以用book = person.person_set.all()
The above query is to query the channel by the sub-table master table.
Now I simply say to query information through the main table child table below.
Suppose I want to query information associated with the author of a book, then I can be achieved through the following steps:
If I were to get a child object table. I just get familiar with the foreign key of the object associated with the main table, you can get the information of the master table.
For example, I get a book object, and then I want to get information master person of the book object, then:
p = book.person
Personally, I feel a bit like a person pointer, the address of the person storing the master table object, Oh! Python but no pointer concept, I think this is better understood.
Explain many to many
1, many (foreign key)
Example: a correspondence of a number of books, one book only one author
model Code:
class Person(models.Model): name = models.CharField( '作者姓名' , max_length= 10 ) age = models.IntegerField( '作者年龄' ) class Book(models.Model): person = models.ForeignKey(Person, related_name= 'person_book' ) title = models.CharField( '书籍名称' , max_length= 10 ) pubtime = models.DateField( '出版时间' )
(A) obtain object methods:
1. From the acquisition of books
person = Person.objects.fiter(你的条件)
book = person.book_set.all()
2. Starting from the acquisition of books
p = book.person
Example: a correspondence of a number of books, a book has multiple authors
model Code:
class Author(models.Model): first_name = models.CharField(max_length= 30 ) last_name = models.CharField(max_length= 40 ) email = models.EmailField() class Book(models.Model): title = models.CharField(max_length= 200 ) authors = models.ManyToManyField(Author)
(A) obtain object methods:
1. Starting from the acquisition of books
b = Book.objects.get(id= 50 ) b.authors.all() b.authors.filter(first_name= 'Adam' )
2. From the acquisition of books
a = Author.objects.get(id= 1 ) a.book_set.all()
(Ii) add an object method:
a = Author.objects.get(id= 1 ) b = Book.objects.get(id= 50 ) b.authors.add(a)
(C) delete the object object methods:
a = Author.objects.get(id= 1 ) b = Book.objects.get(id= 50 ) b.authors.remove(a) 或者 b.authors.filter(id= 1 ).delete()