Python devops Development (20)----models operations, middleware, caching, signaling, paging

Source: Internet
Author: User
Tags html tags

Content Directory

    • Select Form Label Database operations
    • Models Operation f/q
    • Models many-to-many table operations
    • Django Middleware
    • Cache
    • Signal
    • Pagination
Select Form Label Supplement

In the previous section we can know that the form label has two functions, one to do the user submit data validation, one is to generate HTML tags

You can generate a select tag in the generated HTML tag, and select the option data that we can query in the database to read.

class usertype (models. Model):    = models. Charfield (max_length=16)class  UserInfo (models. Model):    = models. Charfield (max_length=32)    = models. Charfield (max_length=32)    = models. ForeignKey ('usertype')#python3 manage.py Makemigrations#python3 Migrate
Models Generating Tables
 fromApp01ImportModelsclassIndexform (forms. Form):#C = [    #(1, ' CEO '),    #(2, ' COO ')    # ]c = Models. UserType.objects.all (). Values_list ('ID','caption') user_type_id= Forms. Integerfield (widget=forms. Select (choices=c))def __init__(Self,*args, * *Kwargs):#Parent Class Construction method: 1, get all static field 2, fields = []Super (Indexform, self).__init__(*args, * *Kwargs)#print (self.fields[' user_type_id '].widget.choices)self.fields['user_type_id'].widget.choices = models. UserType.objects.all (). Values_list ('ID','caption')defIndex (Request):#For I in Range (Ten):    #models. UserType.objects.create (caption= ' CE ' +str (i))    #C = models. UserType.objects.all (). Count ()    #print (c)form =Indexform ()returnRender (Request,'index.html', {'form': Form})
Views Code
<! DOCTYPE html>"en">"  UTF-8">    <title></title>    {{form.user_type_id}}</body>
front-end HTML

Note: After you have finished reading the database data, you start the Django state, and if you insert data into the database, the page does not change because C=models. UserType.objects.all (). Values_list (' id ', ' caption ') belongs to the static object, if you want to see the updated data, you can restart the next Django, if you do not want to restart the need to customize the __init__ construction method.

Models f/q operation
F:temp = Salary+500models. UserInfo.objects.filter (). Update (SALARY=TEMP) update UserInfo set salary=salary+500 from django.db.models Import Fmodels.UserInfo.objects.filter (). Update (Salary=f (' salary ') +500) Q: Constructs the search condition 1, the parameter models. UserInfo.objects.filter (id=123,name= ' Alex ') 2, preach dictionary d = {' id ': 123, ' name ': ' Alex '}models. UserInfo.objects.filter (**d) <input name= ' id '/><input name= ' name '/> Get user input and construct into a dictionary: models. UserInfo.objects.filter (**c) 3, Q object Models.UserInfo.objects.filter (Q object) from django.db.models import q# q1 = Q () # Q1.connector = ' OR ' # q1.children.append ((' id ', 1)) # Q1.children.append ((' id ', 2)) # Q1.children.append ((' ID ', 3)) # Models. Tb1.objects.filter (q1) # con = q () # # q1 = q () # q1.connector = ' OR ' # q1.children.append ((' id ', 1)) # q1.children.append (' id ' , 2) # q1.children.append (' id ', 3) # # q2 = Q () # q2.connector = ' OR ' # q2.children.append (' status ', ' online ') # # Con.add (Q1, ' and ') # Con.add (Q2, ' and ') # # models. Tb1.objects.filter (Con)

Models many-to-many operations

-Create a. Mode one: Class B2G (models. Model): b_id = models. ForeignKey (' boy ') g_id = models. ForeignKey (' Girl ') class Boy (models. Model): Username = models. Charfield (MAX_LENGTH=16) class Girl (models. Model): name = models. Charfield (max_length=16) b. Mode two: Class Boy (Models. Model): Username = models. Charfield (max_length=16) # Girl_setclass Girl (models. Model): name = models. Charfield (max_length=16) b = models. Manytomanyfield (' Boy ')-action: add: Forward G1 = models. Girl.objects.get (id=1) G1.b.add (models. Boy.objects.get (id=1)) G1.b.add (1) bs = models. Boy.objects.all () G1.b.add (*bs) G1.b.add (*[1,2,3]) Reverse B1 = models. Boy.objects.get (id=1) b1.girl_set.add (1) b1.girl_set.add (models. Girl.objects.all ()) B1.girl_set.add (*[1,2,3,4]) ... Delete: G1 = models. Girl.objects.get (id=1) g1.b.clear () # Clears all data associated with Girl id=1 G1.b.remove (2) g1.b.remove (*[1,2]) query: G1 = models. Girl.objects.get (id=1) # Sqlg1.b.all () # Sqlg1.b.filter (). Count () B1 = models. Boy.objects.get (id=1) B1.girl_set.all () models. Girl.objects.all (). VALUES (' id ', ' name ', ' B__username ') models. Boy.objecTs.all (). VALUES (' id ', ' username ', ' girl__name ') Update: Orm:python operation database module: Mysqldbpymysql native sql# from django.db import connection# cursor = connection.cursor () # Cursor.execute ("" "SELECT * from TB WHERE name =%s" "", [' Lennon ']) # row = cursor . Fetchone ()
Django Middleware
Configuration file: Middleware_classes Write Class: process_requestprocess_viewprocess_exceptionprocess_response1.10 profile: MIDDLEWARE = Original version: If there is a return in the Process_request, all the process_response are executed once
Cache

Signal

Page out

Python devops Development (20)----models operations, middleware, caching, signaling, paging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.