The third article finally wrote, the light know those basic queries in the project is no egg use, focus on the Entity Relationship Mapping (ORM), learned today, to record, key words: ForeignKey (many to one), Onetoonefield, Manytomanyfield (Many-to-many)
Entity definition:
1 fromDjango.dbImportModels2 3 #Create your models here.4 classPublisher (models. Model):5Name = models. Charfield (max_length=30)6Address = models. Charfield (max_length=50)7City = models. Charfield (max_length=60)8State_province = models. Charfield (max_length=30)9County = models. Charfield (max_length=50)TenWebsite =models. Urlfield () One A def __str__(self): - returnSelf.name - the classAuthorinfo (models. Model): -Gender =models. Integerfield () -Brithday =models. Datefield () -Join_time =models. Datetimefield () + - def __str__(self): + return 'This gender:'+Str (self.gender) A at - classAuthor (models. Model): -Frist_name = models. Charfield (max_length=30) -Last_Name = models. Charfield (max_length=40) -email =models. Emailfield () -Detail =models. Onetoonefield (Authorinfo) in - def __str__(self): to returnU'%s%s'%(Self.frist_name,self.last_name) + - the * $ classBook (models. Model):Panax Notoginsengtitle = models. Charfield (max_length=100) -Authors =models. Manytomanyfield (Author) thePublisher =models. ForeignKey (Publisher) +Publication_date=models. Datefield () A the def __str__(self): + returnSelf.title - $
Simply drag a model into the NAVICAT:
A publishing house corresponding to many books, book through the foreign key ForeignKey to specify, a book has multiple authors, an author will have more than one book, so here is a many-to-many relationship (Manytomanyfield), an author author will have detailed information, This should be one-to-one relationship (Onetoonefield), the bottom table book_authors is the Manytomanyfield field defined in the book entity is automatically generated, the following to insert data
1 #under Python shell2>>>publisher.objects.create (name='CBS1', address='XXX', city='yyy', state_province='zzz', county=' China', website='Http://www.do-iot.net')3>>>publisher.objects.create (name='CBS2', address='XXX', city='yyy', state_province='zzz', county=' China', website='Http://www.do-iot.net')4>>>publisher.objects.create (name='CBS3', address='XXX', city='yyy', state_province='zzz', county=' China', website='Http://www.do-iot.net')5 6>>>authorinfo.objects.create (gender=1,brithday='2012-03-12', join_time='2013-03-04 12:21:32')7>>>authorinfo.objects.create (gender=0,brithday='2012-03-12', join_time='2013-03-04 12:21:32')8>>>authorinfo.objects.create (gender=1,brithday='2012-03-12', join_time='2013-03-04 12:21:32')9 Ten>>>author.objects.create (frist_name='John', last_name='Leb', email='[email protected]', Detail=authorinfo.objects.get (id=1)) One>>>author.objects.create (frist_name='Susan', last_name='Jeerry', email='[email protected]', Detail=authorinfo.objects.get (id=2)) A>>>author.objects.create (frist_name='Jerry', last_name='Brith', email='[email protected]', Detail=authorinfo.objects.get (id=3)) - ->>>b = Book.objects.create (title='Book1', Publisher=publisher.objects.get (id=1), publication_date='2015-06-30') the>>>b.authors.add (Author.objects.get (id=1)) ->>>b = Book.objects.create (title='Book2', Publisher=publisher.objects.get (id=2), publication_date='2015-06-30') ->>>b.authors.add (Author.objects.get (id=2)) ->>>b = Book.objects.create (title='Book2', Publisher=publisher.objects.get (id=3), publication_date='2015-06-30') +>>>b.authors.add (Author.objects.get (id=3))
One-to-one and one-to-many ways to add the same way, many-to-many need to first add the data in addition to the Manytomanyfield field, and then add Manytomanyfield associated objects on this entity, the original data are inserted well, here is written in the field hand-hit data, there may be bugs, Here's how to look at the query
One to one onetoonefield:
Query user details by User:
1 >>>a = Author.objects.get (id=1)2 >>>d = a.detail3 <authorinfo:this gender:1>
You can also reverse the user details to query the user's basic information:
1 >>>d = AuthorInfo.objects.get (id=1)2 >>>a = d.author3 >>>a4 <author:jack jeeb>
Note here that the reverse query is to use the lower case class name of the object associated with the table
One-to-many foreignkey:
Through the book information query the publisher information:
1 >>>b = Book.objects.get (id=1)2 >>>b.publisher3 <Publisher:cbs1>
Reverse all published books according to the publishing house:
1 >>>p = Publisher.objects.get (id=1)2 >>>b_list = P.book_set.all () 3 >>>len (b_list)4 2
Note that the query here uses the lowercase associated class name + "_set" to query, followed by all () is the same as the normal query, where, order_by and so on
Many-to-many Manytomanyfield:
Search by Book
1 >>>b = Book.objects.get (id=1)2 >>>b.authors.all () 3 [<author:jack jeeb>, <author:susan Leb>]
Reverse the book based on the author's query:
1 >>>a = Author.objects.get (id=1)2 >>>a.book_set.all ()3 [<book:book1>]
Same as a pair of multiple reverse queries.
Understanding these general project data layers is no problem (experience, after all, is the two or three-year-old net development of the Flood B)
PS Reference:
http://logic0.blog.163.com/blog/static/18892814620137343447299/
http://blog.csdn.net/fengyu09/article/details/17434795
Python Django Development 4 ORM