Here's a small piece to bring you a Django common ORM operation detailed. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Django Process:
1 Creating a Django Project: Django-admin startproject ProjectName
2 Creating an app:: Python manage.py startapp appname
3 Create a URL in the controller (urls.py) with the View function mapping relationship (one by one corresponds)
4 Creating a View function to complete the logic code
5 fetching collection objects from the database
5 embedding database variables into templates for rendering (Render method)
6 returning the rendered HTML page to the client
URL: protocol + domain + port + path
Protocol: HTTP
Domain Name: www.cnblogs.com
PORT: 80
Path: yuanchenqi/articles/6811632.html
Data: A=1
The regular expression in the URL configuration matches the path portion of a URL
Tempalte (template): HTML code + logic control code
Logic Control syntax: {{}} render variable filter: {{var| method: parameter}}
{%%} render label
{% if%}
{% for%}
{% URL%}
{% URL%}
Custom Filter and Simpletag:
(1) Create the Templatetags module in the app (required)
(2) Create any. py file, such as: my_tags.py
From Django Import Template
Register = template. Library ()
@register. Filter
def filter_multi (V1,V2):
Return V1 * v2
(3) Create any. py file, such as: my_tags.py
Import the previously created my_tags.py in an HTML file that uses custom Simple_tag and filter: {% load my_tags%}
(4) Using Simple_tag and filter:
{% load xxx%} #首行
# num=12
{{Num|filter_multi:2}} #24
Summarize:
Filter: Only one parameter can be accepted, but the IF statement
Simpletag: can accept multiple parameters, but can not use if and other statements
Orm:
The Relationship between table tables:
A one-to-many foreign key field must be Foreign key in a child table (a one-to-many table)
Many-to-many are implemented in the third table, via two foreign KEY
Adds a unique constraint based on a one-to-one foreign key field.
Using the MySQL method
1 Changing the setting file DB configuration
2 Changing the drive configuration in the __init__ file
ORM to SQL Configuration
Settings inside Configuration loging
Table. Object.filter (): Gets a collection object such as [Obj1,obj2]
Table. Object.get (): A model object was obtained
One-to-many add record:
# method 1:
# Book.objects.create (id=1,title= "Python", publication_date= "2017-03-04", price=88.8,publisher_id=1)
#方法2
P1=publisher.objects.get (name= "NPC Press")
Book.objects.create (id=2,title= "Python", publication_date= "2017-05-04", PRICE=98.8,PUBLISHER=P1)
To create a many-to-many relationship in a models.py file
Authors=models. Manytomanyfield ("Author") #多对多如果表在下方则需要加引号
Many-to-many additions
Manytomany only one way to add:
Book.authors.add (*[author1,author2])
Book.authors.remove (*[author1,author2])
Note: Understanding Book_obj.publisher
Book_obj.authors
Self-built third sheet
Class Book2author (models. Model):
Author=models. ForeignKey ("Author")
book= models. ForeignKey ("book")
# Then there's another way:
Author_obj=models. Author.objects.filter (id=2) [0]
Book_obj =models. Book.objects.filter (id=3) [0]
S=models. Book2Author.objects.create (author_id=1,book_id=2)
S.save ()
S=models. Book2author (author=author_obj,book_id=1)
S.save ()
. Value and. value_list Operation Book Table books
#value的使用 result is not an object but a field or property result of the object is also Queryset
Ret1=book.objects.values (' title ')
Ret1_list = Book.objects.values_list (' title ')
Print (' Ret1 is: ', Ret1) #结果是: Ret1 is: <queryset [{' title ': ' Python '}, {' title ': ' Journey to the Monkey '}, {' title ': ' Python3 '}]>
Print (ret1_list) #结果为querySet里的列表 <queryset [(' Python ',), (' Journey to the Monkey ',), (' Python3 ',)]>
Change the difference between the update and save:
Update is only a set specified field save set all fields, so update is more efficient
Inquire:
Expand Content
# Query Related APIs:
# <1>filter (**kwargs): It contains objects that match the filter criteria given
# <2>all (): Query all results
# <3>get (**kwargs): Returns an object that matches the given filter criteria, returns the result with only one, and throws an error if more than one object meets the filter criteria.
#-----------The following methods are used to process the results of the query: for example, Objects.filter.values ()--------
# <4>values (*field): Returns a valuequeryset--a special queryset that is not instantiated by a series of model objects, but rather an iterative dictionary sequence
# <5>exclude (**kwargs): It contains objects that do not match the given filter criteria
# <6>order_by (*field): Sorting Query Results
# <7>reverse (): Reverse order of query results
# <8>distinct (): Eliminate duplicate records from returned results
# <9>values_list (*field): It is very similar to values (), it returns a tuple sequence, and values returns a dictionary sequence
# <10>count (): Returns the number of objects in the database that match the query (QuerySet).
# <11>first (): Returns the first record
# <12>last (): Returns the last record
# <13>exists (): Returns True if Queryset contains data, otherwise false