Django 中通过Model来定义数据库信息
(models.Model)name models.CharField() (models.Model)username models.CharField() password models.CharField() email models.EmailField() user_type models.ForeignKey()
获取数据的操作有三种
1. all
all ==> 所有数据(序列)
UserInfo.objects.get(username=‘alon‘)
2. get
get ==> 单条数据, obj一个对象(封装一行多列的数据,数据也可以是对象), get(hostname=1)
count = UserInfo.objects.filter(username=username,password=password).count()
3. filter
filter ==> id>5 (序列)
获取id号大于5的数据
UserInfo.objects.filter(id__gt = 5)
UserInfo.objects.filter(user_type__id__gt=5)
UserInfo.objects.filter(user_type__name__contains=‘A‘)
查看用户类型等于"超级管理员"的所有用户
UserInfo.objects.filter(user_type__name="超级管理员")
UserInfo.objects.filter(user_type__name=‘普通用户‘)
select * from UserInfo xxxx
UserInfo.objects.filter(user_type__name="普通用户‘).values(‘username‘, ‘password‘)
select username,password from UserInfo XXXXX
(models.Model)username models.CharField() password models.CharField() email models.EmailField() user_type models.ForeignKey() (models.Model)GroupName models.CharField() user models.ManyToManyField()
多对多表之间
本文出自 “迷荼” 博客,谢绝转载!
原文地址:http://runingday.blog.51cto.com/2154382/1856244