Django--models一对多实例
需求
models一对多表的构建,创建数据,查询数据,数据前端展示等。
速查
1、创建数据
1 2 | all_data = obj.clean() #{'username': u'user1', 'user_group': 1} models.User.objects.create( * * all_data) |
2、数据展示
1 2 | user_list = models.Table.objects. all () #获取表全部数据并传到前端 return render(request, 'html' ,{ 'user_list' :user_list}) |
1 2 | < td >{{ item.普通字段}}</ td > < td >{{ item.外建表对象.外建表字段}}</ td > |
3、外键数据查询
1 2 | list = models.Table.objects. filter (外键表对象__外键表字段 = 外键字段值) return render(request, 'html' ,{ 'list' : list }) |
知识点
ForeignKey对应的是一个对象,是一行数据
添加对象的时候form表单里字段是xx_id
在html里获取外键表的字段数据是用点(.),后台中取数据也是(.)。
在后台中过滤条件(filter)中用双下划线(__),多连接表可用多个双下划线(__)。
详细
1 搭建基本环境
app01/models.py
1 2 3 4 5 6 7 8 9 10 11 | class UserGroup(models.Model): caption = models.CharField(max_length = 64 ) def __unicode__( self ): return self .name class Host(models.Model): hostname = models.CharField(max_length = 64 ) ip = models.GenericIPAddressField() user_group = models.ForeignKey(UserGroup) def __unicode__( self ): return self .name |
app01/urls.py
1 2 3 4 5 | from app01.views import forign urlpatterns = [ url(r '^create_user_group/$' ,forign.create_user_group ), url(r '^create_user/$' ,forign.create_user ), ] |
app01/views/forign.py
1 2 3 4 5 6 | from app01 import models def create_user_group(request): models.UserGroup.objects.create(caption = 'CEO' ) models.UserGroup.objects.create(caption = 'CTO' ) models.UserGroup.objects.create(caption = 'COO' ) return HttpResponse( 'ok' ) |
1 2 3 4 | from app01.forms import forign as ForignForm def create_user(request): obj = ForignForm.CreateUserForm(request.POST) return render(request, 'forign/create_user.html' ,{ 'obj' :obj}) |
templates/forign/create_user.html
1 2 3 4 5 | < form action = "/create_user/" method = "post" > < p >{{ obj.username }}</ p > < p >{{ obj.user_group }}</ p > < input type = "submit" value = "submit" /> </ form > |
app01/forms/forign.py
1 2 3 4 5 6 7 8 9 | from app01 import models class CreateUserForm(forms.Form): username = forms.CharField() user_group = forms.IntegerField( widget = forms.Select() ) def __init__( self , * args, * * kwargs): super (CreateUserForm, self ).__init__( * args, * * kwargs) self .fields[ 'user_group' ].widget.choices = models.UserGroup.objects. all ().values_list( 'id' , 'caption' ) |
browser
访问 http://127.0.0.1:8000/create_user_group/,创建user_group表完成。
访问http://127.0.0.1:8000/create_user/,添加用户。
2 表单提交的数据写到数据库
app01/views/forign.py
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 | from app01.forms import forign as ForignForm def create_user(request): obj = ForignForm.CreateUserForm(request.POST) if request.method = = "POST" : if obj.is_valid(): all_data = obj.clean() #{'username': u'user1', 'user_group': 1} group_obj = models.UserGroup.objects.get( id = all_data[ 'user_group' ]) ''' 第一种方法,对象级别的操作 group_obj是user_group表的一个对象, models.py中user_group列不是一个真正的字段,而是user_group表的一行(对象) 所以我们写user_group=的时候,就要把group_obj这个对象传进去 ''' models.User.objects.create( username = all_data[ 'username' ], user_group = group_obj ) ''' 另一种方法,数据库级别的操作,完全按照数据库中的字段赋值 Django自动把user_group变成了user_group_id,存储的是id,所以直接传id就行了。 ''' # models.User.objects.create( # username = all_data['username'], # user_group_id = all_data['user_group'] # ) return render(request, 'forign/create_user.html' ,{ 'obj' :obj}) |
如果表单提交的数据直接是user_group_id,就可以直接以**dic的方式create了。
app01/forms/forign.py
1 2 3 4 5 6 7 8 9 | from app01 import models class CreateUserForm(forms.Form): username = forms.CharField() user_group_id = forms.IntegerField( #user_group==>user_group_id,下面的构造函数也一样变成user_group_id。 widget = forms.Select() ) def __init__( self , * args, * * kwargs): super (CreateUserForm, self ).__init__( * args, * * kwargs) self .fields[ 'user_group_id' ].widget.choices = models.UserGroup.objects. all ().values_list( 'id' , 'caption' ) |
app01/views/forign.py就简化成了
1 2 3 4 5 6 7 8 | from app01.forms import forign as ForignForm def create_user(request): obj = ForignForm.CreateUserForm(request.POST) if request.method = = "POST" : if obj.is_valid(): all_data = obj.clean() #{'username': u'user1', 'user_group': 1} models.User.objects.create( * * all_data) return render(request, 'forign/create_user.html' ,{ 'obj' :obj}) |
templates/forign/create_user.html 前端也要改成user_group_id
1 | < p >{{ obj.user_group_id }}</ p > |
访问页面添加数据,看数据库变化
3 用户数据展示
app01/views/forign.py
1 2 3 4 5 6 7 8 9 | from app01.forms import forign as ForignForm def create_user(request): obj = ForignForm.CreateUserForm(request.POST) if request.method = = "POST" : if obj.is_valid(): all_data = obj.clean() models.User.objects.create( * * all_data) user_list = models.User.objects. all () #获取user表全部数据并传到前端 return render(request, 'forign/create_user.html' ,{ 'obj' :obj, 'user_list' :user_list}) |
templates/forign/create_user.html
1 2 3 4 5 6 7 8 | < table > {% for item in user_list %} < tr > < td >{{ item.username }}</ td > < td >{{ item.user_group.caption }}</ td > </ tr > {% endfor %} </ table > |
user_group是一个对象,usergroup表的一行数据,所以能取到caption。
browser
4 GET请求
4.1 根据普通字段GET
browser
app01/views/forign.py user_list只包含用户输入的内容
1 2 3 4 5 6 7 8 9 | def create_user(request): obj = ForignForm.CreateUserForm(request.POST) if request.method = = "POST" : if obj.is_valid(): all_data = obj.clean() models.User.objects.create( * * all_data) val = request.GET.get( 'username' ) user_list = models.User.objects. filter (username = val) return render(request, 'forign/create_user.html' ,{ 'obj' :obj, 'user_list' :user_list}) |
4.2 根据外键字段GET
browser
app01/views/forign.py
1 2 3 4 5 6 7 8 9 | def create_user(request): obj = ForignForm.CreateUserForm(request.POST) if request.method = = "POST" : if obj.is_valid(): all_data = obj.clean() models.User.objects.create( * * all_data) val = request.GET.get( 'usergroup' ) #url中的usergroup是参数,什么都成 user_list = models.User.objects. filter (user_group__caption = val) return render(request, 'forign/create_user.html' ,{ 'obj' :obj, 'user_list' :user_list}) |
user_group__caption,对象user_group外联表获取caption字段。