One: Create a table
Model books: books have a title and date of publication, a book may have multiple authors, an author can write more books, so the relationship between the author and the book is the relationship many to many (many-to-many) ;
A book should only be published by a publisher, the publisher and book-to-many relationship (one-to-many).
Create a one to one relationship: OneToOne ( "table to bind relations")
Create a to-many relationship: ( "table name to bind relations") ForeignKey
Create a to-many relationship: ManyToMany ( "table to bind relations") will automatically create a third table
Book class (models.Model): NID = models.AutoField (primary_key = True) # increment id (you can not write, the default will be increased from the above mentioned id) title = models.CharField (max_length = 32) publishDdata = models.DateField ( ) # publication date price = models.DecimalField (max_digits = 5, decimal_places = 2) # a total of five, with two decimal places # Press how a book, write in a related field of multi-party # not named publish_id, django for us because it automatically added _id publish = models.ForeignKey ( "publish") established #foreignkey (table name) many relationship # publish Press the object is associated with an instance of an object authorlist = models.ManyToManyField ( " author ") # built-many relationship def __str __ (self): #__str__ method uses the object into a string Come on, you return to what content can print what return self.title class Publish (models.Model): name = models.CharField (max_length = 32) # id is not written when the database will automatically give you increased increment id addr = models.CharField(max_length=32) def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() class AuthorDeital(models.Model): tel = models.IntegerField() addr = models.CharField(max_length=32) author = models.OneToOneField("Author") #建立的一对一的关系
Note: Fields added temporary data before you should first think there are any. Set a default value.
wordNum = models.IntegerField(default=0)
You can view translated by logging sql statement
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', }, }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'propagate': True, 'level':'DEBUG', }, } }
Precautions:
1, the table name myapp_modelName
is automatically generated based on the metadata model may be overwritten as other name
2, id
the field is automatically added
3, for the foreign key field, Django will be added to the field name "_id" to create the column names in the database
4, in this example CREATE TABLE
SQL statements using PostgreSQL syntax, it should note that Django will use the appropriate SQL statements based on the database type settings specified.
5, after the model has been defined, you need to tell Django _ _ using these models. What you do is modify the configuration file INSTALL_APPSZ setting in which to add models.py
the name of the application.
6, there is a foreign key field ForeignKey provided null = True (which allows the outer space to accept the value NULL), you can assign it a null value None.
Field Options
Each field has a number of specific parameters, e.g., as CharField max_length parameters needed to specify the VARCHAR
size of the database fields. There are some general parameters apply to all fields. These parameters are defined in detail in the document, here we simply introduce some of the most common:
1) null If True, Django will be stored with NULL NULL value in the database. The default is False. (. 1) blank If True, this field is not allowed to fill. The default is False. Note that this is different from null. null purely category database, data validation and blank areas. If a field is blank = True, the validation will allow the form of the field is null. If the field is blank = False, the field is mandatory. (2) default field's default value. It can be a value or a callable object. If you can call, each time a new object is created it will be called. (3) primary_key If True, then the field is the primary key for the model. If you do not specify any field primary_key True =, Django will automatically add a primary key IntegerField field to do so unless you want to override the default behavior of the primary key, otherwise no need to set up a primary_key any field = True. (4) unique If the value is set to True, the value of the data field in the entire table must be unique (5) choices an iterator object is composed of a tuple (e.g., lists or tuples), the fields used to provide options. If the choices,
the default form will be a select box instead of the standard text box, and this option is selected the box choices in options. This is an example of a list of choices: YEAR_IN_SCHOOL_CHOICES = ( ( 'FR', 'Freshman'), ( 'SO', 'Sophomore'), ( 'the JR', 'Junior'), ( 'the SR', 'Senior'), ( 'the GR', 'Graduate'), ) first in each tuple element, the value is stored in the database; the second element is used as the content displayed in the management of interface or ModelChoiceField.
In a given instance of the model class, we want to display the value of a field of choices, call get_FOO_display method (FOO here is the name of choices field). For example: from django.db Import Models class the Person (models.Model): SHIRT_SIZES = ( ( 'S', 'the Small'), ( 'M', 'Medium'), ( 'L', 'Large'), ) = models.CharField name (MAX_LENGTH = 60) shirt_size = models.CharField (= MAX_LENGTH. 1, choices = SHIRT_SIZES) >>> P = the Person (name = " >>> p.get_shirt_size_display() 'Large
Once you establish a good data model, django will automatically generate a database abstraction API, lets you perform additions and deletions of records on the table to change search operation.
Second, add records
Adding many records:
#-Many add # way: If this is specified directly publish_id field to add value, provided that you must have a table inside the main data # main table: not associated (because the book is to rely on the table publish this table a) is publish table # child table: associated table models.Book.objects.create (title = "the kite Runner", publishDdata = "2015-5-8", price = "111", publish_id = 1) # Second way: recommended pub_obj = models.Publish.objects.filter (name = "People's Publishing House") [0] Print (pub_obj) models.Book.objects.create (title = "Jane Eyre", publishDdata = "2000- 6-6 ",. price =" 222 ", publish = pub_obj) # three ways: the Save pubObj = models.Publish.objects.get (name =" People's Publishing House ") # only one time with the get, get directly is an object bookObj = models.Book (title = "true warrior", publishDdata = "2015-9-9", price = "50",publish=pubObj) bookObj.save()
Add-many record:
Books and authors are many relationships: a book can have multiple authors, an author can publish more than one book
Step: Find the book object
Authors need to find objects
Binding of the book object to the object (with the add method), which is bindings many relationship
# Add two ways-many of # one way: # create a book: pub_obj = models.Publish.objects.filter (name = "Universal Press") .first () book_obj = models.Book.objects .create (title = "drunk exquisite", publishDdata = "2015-4-10",. price = "222", publish = pub_obj) # # id by default to find the author's name Django haiyan_obj = models.Author.objects.filter ( = name "Haiyan") [0] egon_obj = models.Author.objects.filter (name = "Egon") [0] xiaoxiao_obj = models.Author.objects.filter (name = "xiaoxiao") [0] # bound many relationship, book_obj.authorlist.add (haiyan_obj, egon_obj, xiaoxiao_obj) # Second way =========, identify all authors pub_obj = models.Publish.objects.filter (name = "omnipotent Press ") .first () book_obj = models.Book.objects.create (title = "drunk Delicate", publishDdata = "2015-4-10",. price = "222", publish = pub_obj) authers = models.Author.objects. All () # # binding to-many relationship book_obj.authorlist.add ( * authers )
Unbind: remove:
# 将某个特定的对象从被关联对象集合中去除。 ====== book_obj.authors.remove(*[])
# Lift-many relationship (the Remove) book_obj = models.Book.objects.filter (title = "drunk exquisite") .last () # find the book object authers = models.Author.objects.filter (id__lt = 3) # find qualified authors objects book_obj.authorlist.remove (* authers) # is as clear and more, we have to add *
Clear Binding: clear "
#清空被关联对象集合。
# Clear the relational approach (the Clear) book_obj = models.Book.objects.filter (title = "Dream of Red Mansions") for book_obj_item in book_obj: # Dream of Red Mansions gave all cleared book_obj_item.authorlist.clear ()
Summary: remove and clear the difference between
remove: Are you going to have to clear the data filter out, and then remove the
clear: do not check, put the data directly cleared.
Each scenario
Third, based on the query record object (where the child is equivalent to sql statement query)
One query record : author and authordetile is one to one relationship
Forward queries (by field author)
Reverse lookup (by table name authordeital): Because it is one to one relationship, and would not _set up.
# One query # forward query: phone number of the 13,245 names deital_obj = models.AuthorDeital.objects.filter (tel = "13245") First (). Print (deital_obj.author.name) # reverse query: query egon phone number . egon_obj = models.Author.objects.filter (name = "egon") First () Print (egon_obj.authordeital.tel)
Many query record:
Forward queries (by field: publish):
Reverse lookup (by table name: book_set):
# Forward queries: Queries true warrior book publishers address
book_obj = models.Book.objects.filter (title = "true warrior") [0] # find the object
print ( "====== ", book_obj.publish) # get the object is associated press
Print (book_obj.publish.addr)
# reverse Search: Find all the books of the People's Publishing House published price and the name
pub_obj = models.Publish.objects .filter (name = "Publishing House") [0]
book_dic pub_obj.book_set.all = (). values ( ". price", "title") [0]
Print (book_dic)
Print (book_dic [ ". price"])
# 查询 人民出版社出版过的所有书籍
publish
=models.
Publish.objects.get(name
=
"人民出版社"
) #get得到的直接是一个对象,不过get只能查看有一条记录的
book_list
=
publish.book_set.
all
()
# 与人民出版社关联的所有书籍对象集合
for
book_obj
in
book_list:
print
(book_obj.title)
Note the use for recycling or use values, vauleslist are possible.
Many to many query record:
Forward queries (by field authorlist)
Reverse lookup (by table name book_set)
#-Many queries # Forward Search: Find people Kite Runner of the book all the author's name and age book_obj = models.Book.objects.filter (title = "Dream of Red Mansions") [0] Print (book_obj . .authorlist.all () values ( "name ", "age")) # this book is associated with all of the objects in the collection # reverse lookup: query author is haiyan this person out of several books which information haiyan_obj models.Author.objects.filter = (name = "Haiyan") [0] Print ( "BookInfo ====", haiyan_obj.book_set.all (). First (). title) # associated with the authors of all the books collection of objects return HttpResponse ( "ok")
You can set the value related_name to override the name FOO_set by definition ForeignKey () and ManyToManyField in. For example, if you do some changes in the Article model: publish = ForeignKey (Blog, related_name = 'bookList'), then the next, as we will see so:
# Inquiry People's Publishing House published all the books publish = Publish.objects.get (name = "People's Publishing House") book_list = publish.bookList.all () # associated with the People's Publishing House collection of objects of all books
Fourth, based on cross-table double-underlined inquiry
Django also provides an intuitive and efficient way to express relationship in the query (lookups), it can automatically confirm SQL JOIN contact. Do cross-relational query, use two to underscore the link between the name of the model (model) associated field until the final link until you want to model. (Equivalent manner join connection, which may be provided with the sql statement settings, view sql statement)
Many query:
Exercise 1, the price of all the books of the inquiry People's Publishing House published the names and
Based on double underline # 1 way to query many ================ # The first search method ret = models.Publish.objects.filter (name = "People's Publishing House" ) .values ( "book__price", "book__title") Print (RET) # second search method ret2 = models.Book.objects.filter (publish__name = "People's Publishing House") .values ( "price", "title" ) Print (RET2)
Exercise 2, the query linux book publishers address: filer first filter ,, field values display requirements
The first search method RET = models.Book.objects.filter (title = "Linux"). Values ( "publish__addr") Print (RET) second search method ret2 = models.Publish.objects.filter (book__title = " Linux "). values (" addr ") Print (RET2)
Many to many query:
Exercise 1 query name egon been out of all the books,
# A way
RET = models.Author.objects.filter (name = "Egon") values ( "book__title"). Print (RET)
# Second way: two ways that is not the same logic ret2 = models.Book.objects. filter (authorlist__name = "Egon"). values ( "title") Print (RET2)
Exercise 2, the query name as mobile phone number published in the beginning of the 151 authors of all the books and publishers
# 方式一: author_obj = models.AuthorDeital.objects.filter(tel__startswith="151").first() print(author_obj.author.book_set.all().values("title","publish__name")) # 方式二: ret = models.Book.objects.filter(authorlist__author_deital__tel__startswith="151").values("title","publish__name") print(ret)
V. aggregate queries and grouping queries (very important !!!)
Aggregate queries: Aggregate (* args, ** kwargs), only one polymerizable group
Avg django.db.models Import from, Sum, the Count, Max, Min
# 1, query the average price of all books print (models.Book.objects.all (). aggregate ( Avg ( "price")))
aggregate () is QuerySet of a termination clause (that is, no longer is the time to return a QuerySet collection), meaning that it returns a dictionary that contains a number of key-value pairs. The aggregate value is the name of the key identifier value is computed aggregate values. Name of the key is automatically generated out according to the name field and aggregate functions. If you want to specify a name for the aggregate values, it can provide to the aggregation clause.
Avg django.db.models Import from, Sum, the Count, Max, Min # 1, query the average price of all books print (models.Book.objects.all (). aggregate ( avgprice = Avg ( "price")))
If you want to generate more than one aggregate, you may provide Aggregate () clause add another parameter. So, if you want to know all the books of the maximum and minimum prices, it can be a query:
print(models.Book.objects.all().aggregate(Avg("price"),Max("price"),Min("price")))
#打印的结果是: {'price__avg': 174.33333333333334, 'price__max': Decimal('366.00'), 'price__min': Decimal('12.00')}
Query packet: annotate (): is QuerySet each separate object generates a summary value.
The results of the polymerization is complete performed after packet
1, count the number of each book
# 方式一: print(models.Book.objects.all().annotate(authorNum = Count("authorlist__name")).values("authorNum")) # 方式二: booklist =models.Book.objects.all().annotate(authorNum=Count("authorlist__name")) for book_obj in booklist: print(book_obj.title,book_obj.authorNum)
2, each statistical cheapest book publishing house
# 2, the statistics for each book the cheapest Press # a way: Print (models.Book.objects.values ( "publish__name") Annotate (nMinPrice = Min ( '. Price')).) Note: The values in the field i.e. the group by the grouping condition field ,, i.e. # Second way: Print (. models.Publish.objects.all () Annotate (minprice = Min ( "book__price")) values ( "name", "minprice").) # three ways publishlist = models.Publish.objects.annotate (minprice = Min ( "book__price")) for publish_obj in publishlist: Print (publish_obj.name, publish_obj.minprice)
3, count the number of each book to py the beginning of the book:
print(models.Book.objects.filter(title__startswith="py").annotate(authNum = Count("authorlist__name")).values("authNum"))
(4) the statistics of more than a book:
print(models.Book.objects.annotate(num_authors=Count('authorlist__name')).filter(num_authors__gt=1).values("title","num_authors"))
(5) of the query set QuerySet are ordered according to how much one of a number of books:
print(models.Book.objects.all().annotate(authorsNum=Count("authorlist__name")).order_by("authorsNum"))
Total price (6) queries each of the book:
# 方式一 print(models.Author.objects.all().annotate(priceSum = Sum("book__price")).values("name","priceSum")) # 方式二 print(models.Book.objects.values("authorlist__name").annotate(priceSum=Sum("price")).values("authorlist__name","priceSum"))