ããã«ã¡ã¯ãPyQãµãã¼ãã§ãã
2022å¹´8æ3æ¥ã«Django 4.1ããªãªã¼ã¹ããã¾ããã
ãã®è¨äºã§ã¯Django 4.1ã®ã¢ãããã¼ãå
容ããã次ã®é
ç®ã«ã¤ãã¦ãç´¹ä»ãã¾ãã
- éåæ対å¿
- ã¯ã©ã¹ãã¼ã¹Viewã®éåæ対å¿
- ORMã®éåæã¤ã³ã¿ã¼ãã§ã¼ã¹æä¾
- ã¢ãã«ãã©ã¼ã ã®ããªãã¼ã·ã§ã³ã«Meta.constraintsã®ãã§ãã¯ã追å
- ãã©ã¼ã ã¬ã³ããªã³ã°ãdivãã¼ã¹ã«å¤æ´ããããã®æºå
- CSRF_COOKIE_MASKED
ä»ã«ãå¤ãã®å¤æ´ãããã¾ãã
ãã詳ããç¥ãããæ¹ã¯Django 4.1ã®ãªãªã¼ã¹ãã¼ãããèªã¿ãã ããã
- Django 4.1 ãªãªã¼ã¹ã®æ¦è¦
- éåæ対å¿
- ã¢ãã«ãã©ã¼ã ã®ããªãã¼ã·ã§ã³ã«Meta.constraintsã®ãã§ãã¯ã追å
- ãã©ã¼ã ã¬ã³ããªã³ã°ãdivãã¼ã¹ã«å¤æ´ããããã®æºå
- CSRF_COOKIE_MASKED
- ãã®ä»ã®å¤æ´
- ã¾ã¨ã
Django 4.1 ãªãªã¼ã¹ã®æ¦è¦
å称 | Django |
---|---|
ãªãªã¼ã¹ãã¼ã¸ã§ã³ | Django 4.1 |
ãªãªã¼ã¹æ¥ | 2022å¹´8æ3æ¥ |
ãµãã¼ããã¦ããPython | 3.8ã3.9ã3.10 |
ãªãªã¼ã¹ãã¼ã | https://docs.djangoproject.com/ja/4.1/releases/4.1/ |
â»ã¢ãããã¼ãã®éã¯Backwards Incompatible Changesï¼å¾æ¹äºææ§ã®ãªãå¤æ´ï¼ã«åå注æãã¦ãã ãã
éåæ対å¿
Django 4.1ã§ã¯ã以ä¸ã®éåæ対å¿ãè¡ããã¾ããã
- class-based viewsã®éåæ対å¿
- ORMã®éåæã¤ã³ã¿ã¼ãã§ã¼ã¹æä¾
Django 3.0ã®æç¹ã§éåæ対å¿ã®Webãµã¼ãã¼ï¼ASGIãµã¼ãã¼ï¼ã§åä½ããããã«ãªããDjango 3.1ã§ã¯function-based viewã¸ã®éåæ対å¿ãè¡ããã¾ããã
ããããclass-based viewsãORMãªã©ã¯éåæã«å¯¾å¿ãã¦ãã¾ããã§ããã
ä»åã®ã¢ããã°ã¬ã¼ãã§ãããã®éåæ対å¿ãè¡ããã¾ããã
Django 3.0ã¨3.1ã§ã®éåæ対å¿ã«ã¤ãã¦ã¯ãéå»ã®è¨äºã御覧ãã ããã
ã¯ã©ã¹ãã¼ã¹Viewã®éåæ対å¿
ä»åã¯ãã¯ã©ã¹ãã¼ã¹Viewã§ãéåæ対å¿ãã¾ããã
以ä¸ã®ããã«ã¯ã©ã¹ãã¼ã¹Viewã®ã¡ã½ããå®ç¾©ã« async def
ã使ããããã«ãªãã¾ãããï¼Django 4.1ã®ãªãªã¼ã¹ãã¼ãããå¼ç¨ï¼
import asyncio from django.http import HttpResponse from django.views import View class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform view logic using await. await asyncio.sleep(1) return HttpResponse("Hello async world!")
ORMã®éåæã¤ã³ã¿ã¼ãã§ã¼ã¹æä¾
ä»åã®å¤æ´ã§ãQuerySetã®ãã¹ã¦ã®ãã¼ã¿ã¢ã¯ã»ã¹æä½ã«å¯¾ãã¦éåæã¤ã³ã¿ã¼ãã§ã¼ã¹ãæä¾ããããã«ãªãã¾ããã
ãããã¯ãæ¢åã®åææä½ï¼create(), get()ãªã©ï¼ã«æ¥é è¾ a
ï¼acreate(), aget()ãªã©ï¼ãã¤ãã¦ãã¾ãã
æ°ããã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
ã«ããã sync_to_async()
ã§ã©ããããã«éåæã³ã¼ããå®è£
ã§ããããã«ãªãã¾ããã
ãã ããä»åã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®æä¾ã®ã¿ã§ããã¼ã¿ãã¼ã¹æä½ã¯åæå¦çã®ã¾ã¾ã§ãã
ç¾å¨ã¯ãæ°ããã¡ã½ããå
ã« sync_to_async()
å¦çãå
å
ãã¦ããç¶æ
ã§ãéåæãã¼ã¿ãã¼ã¹ãã©ã¤ãã¼ã®çµè¾¼ã¿ã¯ç¾å¨é²è¡ä¸ã¨ã®ãã¨ã§ãã
Djangoã®éåæ対å¿ã®é²æã楽ãã¿ã§ããã
ã¢ãã«ãã©ã¼ã ã®ããªãã¼ã·ã§ã³ã«Meta.constraintsã®ãã§ãã¯ã追å
ããã¾ã§ãã¢ãã«ã®Metaã«constraintsãè¨å®ãã¦ãã¦ããã¼ã¿ãã¼ã¹ä¸ã§å¶éããã ãã§ããã©ã¼ã ã§ããªãã¼ã·ã§ã³ããã«ã¯ããã©ã¼ã å´ã«ããªãã¼ã¿ã¼ãå®è£
ããå¿
è¦ãããã¾ããã
ä»åã®å¤æ´ã§ãã¢ãã«ãã©ã¼ã ã®ããªãã¼ã·ã§ã³ä¸ã«Meta.constraintsã®ãã§ãã¯ãè¡ãããããã«ãªããå¶éã«åãã㨠ValidationError
ãèµ·ããããã«ãªãã¾ããã
Meta.constraintsã«ã¤ãã¦ã¯ãéå»ã®è¨äºã§è©³ããç´¹ä»ãã¦ãã¾ãã
ãã©ã¼ã ã¬ã³ããªã³ã°ãdivãã¼ã¹ã«å¤æ´ããããã®æºå
ç¾å¨ã {{ form }}
ã§åºåãããããã©ã«ãã®ãã³ãã¬ã¼ãã¯ãtableãã¼ã¹ã®ãã³ãã¬ã¼ãã§ãã
ããããé³å£°èªã¿ä¸ãã½ããããã®ä»ã®æ¯æ´æè¡ã使ç¨ããã¦ã¼ã¶ã¼ã®ããã«ãä»åããdivãã¼ã¹ã®ãã©ã¼ã ãã³ãã¬ã¼ãã使ç¨ã§ããããã«ãªãã¾ããã
æ°ãããã³ãã¬ã¼ãã使ãããã«æ¨å¥¨ããã¦ãããDjangoããã¼ã¸ã§ã³5.0ããã¯divãã¼ã¹ã®ãã³ãã¬ã¼ããããã©ã«ãã§ä½¿ããã¨ã«ãªãã¾ããã
ãã®ä¸ç°ã¨ãã¦ãä»åãFormsã¨FormSetsã«ãã³ãã¬ã¼ã div.html
ã追å ããã¾ããã
ã¾ãããã¼ã¸ã§ã³5.0ã¾ã§ã®éã«ãããã¸ã§ã¯ãã¬ãã«ã§ããã©ã«ããã³ãã¬ã¼ããæå®ã§ããããã« FORM_RENDERER
ãç¨æããã¾ããã
以ä¸ã®ããã«è¨å®ããã¨ãããã©ã«ããã³ãã¬ã¼ãã div.html
ã«ãªãã¾ãã
ãã® FORM_RENDERER
ã¯ã移è¡ç¨ã§ãã
ãã¼ã¸ã§ã³5.0ã§éæ¨å¥¨ã«ãªãããã¼ã¸ã§ã³6.0ã§åé¤ãããäºå®ã§ãã
FORM_RENDERER = "django.forms.renderers.DjangoDivFormRenderer"
CSRF_COOKIE_MASKED
æ°ãã移è¡ç¨è¨å® CSRF_COOKIE_MASKED
ã追å ããã¾ããã
CsrfViewMiddleware ããCSRF cookieããã¹ã¯ããªããªãã¾ããã
ãã®ãããå¤ãDjangoã®ãã¼ã¸ã§ã³ãå¤æ´ãªãã«4.1ã«ã¢ãããã¼ãããããã«ã¯ã CSRF_COOKIE_MASKED
ã« True
ãæå®ããå¿
è¦ãããã¾ãã
CSRF_COOKIE_MASKED
ã¯ç§»è¡ç¨ã§ãã®ã§ããã¼ã¸ã§ã³5.0ã§åé¤ããã¾ããããã¾ã§ã«ã¯æ°ãããã¼ã¸ã§ã³ã«å¯¾å¿ããå¿
è¦ãããã¾ãã
ãã®ä»ã®å¤æ´
以ä¸ã®ãããªå¤æ´ãããã¾ãã
Form.as_div()
ã追å ããã<div>
ã使ã£ããã©ã¼ã ãèªåã§ä½ããããã«ãªãã¾ãã- PostgreSQL 10ã®ãµãã¼ãããªããªãã¾ãã
- MariaDB 10.2ã®ãµãã¼ãããªããªãã¾ãã
- çµè¾¼ã¿ã®
logout view
ã§GETãªã¯ã¨ã¹ããéæ¨å¥¨ã«ãªããPOSTãªã¯ã¨ã¹ãã«ãªãã¾ãã
ä»ã«ããããããªå¤æ´ãããã¾ãã
詳ããã¯ãDjango 4.1ã®ãªãªã¼ã¹ãã¼ããåç
§ãã ããã
ã¾ã¨ã
ä»åã®ãã¼ã¸ã§ã³ã¢ããã§ãéåæå¦çã¸ã®å¯¾å¿ãé²ã¿ã¾ããã
ã¾ãã {{ form }}
㧠<div>
ãåºåãããããã«ãªãã¾ãããä»ã«ããã¼ã¸ã§ã³5.0ã¸åãã¦ã®æºåãè¡ããã¾ããã
次ã®ãªãªã¼ã¹ã楽ãã¿ã§ããã