Python Django Database Add query
Working with Data
First, create a record
# pwd
/root/csvt03
# Ipython manage.py Shell
In [1]: From blog.models import Employee
# (First method)
In [2]: Employee
OUT[2]: blog.models.Employee
In [3]: emp = Employee ()
In [4]: emp.name = ' Alen '
In [5]: emp.save ()
# (second method)
In [6]: emp = Employee (name= ' Tom ')
In [7]: emp.save ()
# (Third method)
Call Manager Create
In [8]: Employee.objects.create (name= ' Max ')
Query database has created a record
Second, the inquiry record
# Ipython manage.py Shell
In []: emps = Employee.objects.all ()
In []: emps
OUT[14]: [<employee:employee object>, <employee:employee object>, <employee:employee Object>, < Employee:employee Object>]
in [+]: emps[0].id
OUT[16]: 1L
in [+]: emps[0].name
OUT[17]: U ' Alen '
In []: emps[1].name
OUT[18]: U ' Tom '
in [+]: emps[2].name
OUT[19]: U ' Max '
# Cat blog/models.py
From django.db import Models
Class Employee (models. Model):
Name = models. Charfield (MAX_LENGTH=20)
def __unicode__ (self): # Displays the queried data as a string by __unicode__
Return Self.name
# Ipython manage.py Shell
In [1]: From blog.models import Employee
In [2]: emp = Employee.objects.all ()
In [3]: EMP
OUT[3]: [<employee:alen>, <employee:tom>, <employee:max>, <employee:sumer>]
Third, the delivery to the Web page display query results
# Add URL, and add index.html template file
# egrep-v "#|^$" urls.py
From django.conf.urls.defaults import patterns, include, url
Urlpatterns = Patterns ("',
URL (r ' ^index/$ ', ' Blog.views.index '),
)
# egrep-v "#|^$" blog/models.py
From django.db import Models
Class Employee (models. Model):
Name = models. Charfield (MAX_LENGTH=20)
def __unicode__ (self):
Return Self.name
# egrep-v "#|^$" blog/templates/index.html
<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>
<title>loyu Django test</title>
<body>
{% for EMP in emps%}
<div>{{forloop.counter}}:{{emp}}</div>
{% ENDFOR%}
<div> Total Records </div>
</body>
Iv. Start-Up project
# nohup python manage.py runserver & (Background boot with nohup support)
This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1850751
Viii. Python Django Database Add query