1、數據庫操做html
基本結構python
1
2
3
4
5
6
|
from
django.db
import
models
class
userinfo(models.Model):
#若是沒有
models.AutoField,默認會建立一個id的自增列
name
=
models.CharField(max_length
=
30
)
email
=
models.EmailField()
memo
=
models.TextField()
|
1
2
3
4
|
class
UserInfo(models.Model):
name
=
models.CharField(max_length
=
32
)
ctime
=
models.DateTimeField(auto_now
=
True
)
uptime
=
models.DateTimeField(auto_now_add
=
True
)
|
1
2
3
4
5
6
|
from
web
import
models
def
home(request):
models.UserInfo.objects.create(name
=
'yangmv'
)
after
=
models.UserInfo.objects.
all
()
print
after[
0
].ctime
return
render(request,
'home/home.html'
)
|
ip = models.GenericIPAddressField(protocol="ipv4",null=True,blank=True)
img = models.ImageField(null=True,blank=True,upload_to="upload")
class UserInfo(models.Model):
USER_TYPE_LIST = (
(1,'user'),
(2,'admin'),
)
user_type = models.IntegerField(choices=USER_TYPE_LIST,default=1)
二、連表結構web
應用場景:sql
- 一對多:當一張表中建立一行數據時,有一個單選的下拉框(能夠被重複選擇)
例如:建立用戶信息時候,須要選擇一個用戶類型【普通用戶】【金牌用戶】【鉑金用戶】等。- 多對多:在某表中建立一行數據是,有一個能夠多選的下拉框
例如:建立用戶信息,須要爲用戶指定多個愛好- 一對一:在某表中建立一行數據時,有一個單選的下拉框(下拉框中的內容被用過一次就消失了
例如:原有含10列數據的一張表保存相關信息,通過一段時間以後,10列沒法知足需求,須要爲原來的表再添加5列數據
1
2
3
4
5
6
|
class
Game(models.Model):
gname
=
models.CharField(max_length
=
32
)
class
Host(models.Model):
hostname
=
models.CharField(max_length
=
32
)
game
=
models.ForeignKey(
'Game'
)
|
1
2
3
4
5
6
7
8
|
class
UserGroup(models.Model):
group_name
=
models.CharField(max_length
=
16
)
class
User(models.Model):
name
=
models.CharField(max_length
=
16
)
sex
=
models.CharField(max_length
=
16
)
email
=
models.EmailField(max_length
=
32
)
usergroup_user
=
models.ManyToManyField(
'UserGroup'
)
|
1
2
3
4
5
6
7
8
9
|
class
User2(models.Model):
name
=
models.CharField(max_length
=
16
)
sex
=
models.CharField(max_length
=
16
)
email
=
models.EmailField(max_length
=
32
)
class
Admin(models.Model):
username
=
models.CharField(max_length
=
32
)
password
=
models.CharField(max_length
=
32
)
admin_user2
=
models.OneToOneField(
'User2'
)
|
1
2
3
4
5
6
7
8
|
from
django.db
import
models
class
Group2(models.Model):
caption
=
models.CharField(max_length
=
32
)
class
User2(models.Model):
username
=
models.CharField(max_length
=
32
)
group2
=
models.ForeignKey(
'Group2'
)
|
先執行create_group生成3個group數據庫
已經查詢出Group數據django
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def
create_user(request):
obj
=
Forign.UserForm(request.POST)
if
request.method
=
=
'POST'
:
if
obj.is_valid():
all_data
=
obj.clean()
#print all_data
#獲取提交頁面提交來的數據{'username': u'yang1', 'usergroup': 1}
#方法1,先獲取對象,添加
#group_obj = models.Group2.objects.get(id=all_data['usergroup'])
#models.User2.objects.create(username=all_data['username'],usergroup=group_obj)
#方法2(推薦)
models.User2.objects.create(username
=
all_data[
'username'
],group2_id
=
all_data[
'usergroup'
])
#django會自動把數據庫group2變爲group2_id
else
:
error
=
obj.errors
print
error[
'username'
][
0
]
print
error[
'usergroup'
][
0
]
return
render(request,
'forign/create_user.html'
,{
'obj'
:obj})
|
方法3app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def
create_user(request):
obj
=
Forign.UserForm(request.POST)
if
request.method
=
=
'POST'
:
if
obj.is_valid():
all_data
=
obj.clean()
#print all_data
#獲取提交頁面提交來的數據{'username': u'yang1', 'usergroup': 1}
#方法1,先獲取對象,添加
#group_obj = models.Group2.objects.get(id=all_data['usergroup'])
#models.User2.objects.create(username=all_data['username'],usergroup=group_obj)
#方法2(推薦)
#models.User2.objects.create(username=all_data['username'],group2_id=all_data['usergroup'])
#django會自動把數據庫group2變爲group2_id
#方法3(推薦)
models.User2.objects.create(
*
*
all_data)
print
models.User2.objects.
all
().count()
else
:
pass
# error = obj.errors
# print error['username'][0]
# print error['usergroup_id'][0]
return
render(request,
'forign/create_user.html'
,{
'obj'
:obj})
|
查詢。展現出全部的數據spa
1
2
3
4
5
6
7
8
9
10
11
12
|
def
create_user(request):
obj
=
Forign.UserForm(request.POST)
if
request.method
=
=
'POST'
:
if
obj.is_valid():
all_data
=
obj.clean()
#方法3(推薦)
models.User2.objects.create(
*
*
all_data)
print
models.User2.objects.
all
().count()
else
:
pass
user_list
=
models.User2.objects.
all
()
return
render(request,
'forign/create_user.html'
,{
'obj'
:obj,
'user_list'
:user_list})
|
1
2
3
4
5
6
7
8
|
<
table
border="1">
{% for item in user_list %}
<
tr
>
<
td
>{{ item.username }}</
td
>
<
td
>{{ item.group2.caption }}</
td
>
</
tr
>
{% endfor %}
</
table
>
|
GET方式查詢
3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
def
create_user(request):
obj
=
Forign.UserForm(request.POST)
if
request.method
=
=
'POST'
:
if
obj.is_valid():
all_data
=
obj.clean()
#方法3(推薦)
models.User2.objects.create(
*
*
all_data)
print
models.User2.objects.
all
().count()
else
:
pass
#查用戶
get_user
=
request.GET.get(
'username'
)
user_list
=
models.User2.objects.
filter
(username
=
get_user)
return
render(request,
'forign/create_user.html'
,{
'obj'
:obj,
'user_list'
:user_list})
|
1
2
3
|
#查組
get_val
=
request.GET.get(
'group'
)
user_list
=
models.User2.objects.
filter
(group2__caption
=
get_val)
|