基本结构
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
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# 获取个数
#
# models.Tb1.objects.filter(name='seven').count()
# 大于,小于
#
# models.Tb1.objects.filter(id__gt=1) # 获取id大于1的值
# models.Tb1.objects.filter(id__lt=10) # 获取id小于10的值
# models.Tb1.objects.filter(id__lt=10, id__gt=1) # 获取id大于1 且 小于10的值
# in
#
# models.Tb1.objects.filter(id__in=[11, 22, 33]) # 获取id等于11、22、33的数据
# models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in
# contains
#
# models.Tb1.objects.filter(name__contains="ven")
# models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感
# models.Tb1.objects.exclude(name__icontains="ven")
# range
#
# models.Tb1.objects.filter(id__range=[1, 2]) # 范围bettwen and
# 其他类似
#
# startswith,istartswith, endswith, iendswith,
# order by
#
# models.Tb1.objects.filter(name='seven').order_by('id') # asc
# models.Tb1.objects.filter(name='seven').order_by('-id') # desc
# limit 、offset
#
# models.Tb1.objects.all()[10:20]
# group by
from
django.db.models
import
Count,
Min
,
Max
,
Sum
# models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))
# SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
|
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)
2、连表结构
应用场景:
- 一对多:当一张表中创建一行数据时,有一个单选的下拉框(可以被重复选择)
例如:创建用户信息时候,需要选择一个用户类型【普通用户】【金牌用户】【铂金用户】等。- 多对多:在某表中创建一行数据是,有一个可以多选的下拉框
例如:创建用户信息,需要为用户指定多个爱好- 一对一:在某表中创建一行数据时,有一个单选的下拉框(下拉框中的内容被用过一次就消失了
例如:原有含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
9
10
|
DATABASES
=
{
'default'
:{
'ENGINE'
:
'django.db.backends.mysql'
,
'NAME'
:
'dbname'
,
'USER'
:
'root'
,
'PASSWORD'
:
'xxx'
,
'HOST'
:'',
'PORT'
:'',
}
}
|
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数据
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})
|
方法3
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})
|
查询。展示出所有的数据
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方式查询
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)
|