1. Saving between many-to-many tables
Way One:
U2 = User.objects.get (id=2) g2 = Group.objects.get (id=2) U2.group_relation.add (G2) #在关系表中存入关联数据
Way two:
name = Request. Post.get (' name ', None) Username = Request. Post.get (' username ', None) password = Request. Post.get (' password ', None) gplist = Request. Post.getlist (' groupId ') user = Users.objects.create (name=name,username=username,password=password) User.save () Glist = UserGroup.objects.filter (id__in=gplist) User.groupId.add (*glist) # or #for Gord in gplist:# group = UserGroup.objects.get (Id=gord) # User.groupId.add (group)
Way three:
U2 = User.objects.get (id=2) g2 = Group.objects.get (id=2) G2.user_set.add (U2) #无多对多字段方添加关联, _set is a fixed collocation, It is preceded by the lowercase of the class name of the class that contains many-to-many fields
2. Data acquisition between many-to-many tables
U2 = User.objects.get (id=2) g2 = Group.objects.get (id=2) print (U2.group_relation.all ()) print (U2.group _relation.all ()). Filter (id=1) print (U2.group_relation.all ()). Filter (caption= ' CEO ')
3. Session
A) request.session[' is_login '] = True with session value b) Is_login = Request.session.get (' Is_login ', None) Get session value C) Del request.session[' Is_login '] destroy Sessiond) settings.py set session_cooke_age=10 settings session10 seconds expire auto destroy I. Session_expire_at_browser_close set whether session is destroyed as the browser shuts down
4. Cooke
Response = Render_to_response (' index.html ', ret) Response.set_cookie (' key ', ' value ') #设置cookeprint request. COOKIES #获取cookierequest. Cookies.get (' Page_num ', 10)
Django Basics (ii)