Batch query and storage of python Databases

Source: Internet
Author: User

# Create your views here.
#-*-Coding: UTF-8 -*-
From django. template import loader, Context, Template
From django. http import HttpResponse
From models import User
From django. shortcuts import render_to_response
Def index (req ):
# Batch query of data
StartId = 1
EndId = 100
NumId = (endId-startId) + 1
Numi = numId/20
AddnumId = numId % 20

For I in range (numi ):
Idstart = startId
Idend = I * 20
User = User. objects. filter (pk _ gt = 0). filter (pk _ lt = 5)
Print user. values ()
If len (user)> 0:
Print 'fff'
Else:
Print 'kong'
# Print user
Data1 = []
Data = []
For key in user:
Data1.append (key. name)
Data1.append (key. age)
Data1.append (key. sex)
# Print data1
Data. append (data1)
Data1 = []


# Batch save
# For user in range data:
# User = user
# Data = [['Li si', 12, 'female '], ['wang wu', 56, 'male'], ['zhao liu', 25, 'male']
Stu = User ()
For user in data:
Print user
# User. objects. create (name = user [0], age = user [1], sex = user [2])
# User. objects. create (user)
# User. save ()




# Print data
Return render_to_response('index.html ',{''})


Def instance (startId, endId ):
# StartId = 1
# EndId = 100
NumId = (endId-startId) + 1
Numi = numId/20
AddnumId = numId % 20
If addnumId> 0:
Numi + = 1
For I in range (numi ):
If I = 0:
Idstart = startId
Idend = (I + 1) * 20 + startId
Print Idstart, Idend
Elif I = (numi-1 ):
Idstart = startId + (I * 20)
Idend = endId
Print Idstart, Idend
Else:
Idstart = startId + (I * 20)
Idend = (I + 1) * 20 + startId
Print Idstart, Idend

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.