Django Basics (ii)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.