ããã«ã¡ã¯ãPyQãµãã¼ãã§ãã
2021å¹´12æ7æ¥ã«Django 4.0ããªãªã¼ã¹ããã¾ããã
ãã®è¨äºã§ã¯Django 4.0ã®ã¢ãããã¼ãå
容ããã次ã®é
ç®ã«ã¤ãã¦ãç´¹ä»ãã¾ãã
- Redisã®ãã£ãã·ã¥ããã¯ã¨ã³ããçµè¾¼ã¿ã§ãµãã¼ã
- zoneinfoãããã©ã«ãã®ã¿ã¤ã ã¾ã¼ã³å®è£ ã«
- ãã©ã¼ã ã¯ã©ã¹ã«ãã³ãã¬ã¼ããæå®ããå±æ§ã追å
ä»ã«ãå¤ãã®å¤æ´ãããã¾ãããã詳ããç¥ãããæ¹ã¯Django 4.0ã®ãªãªã¼ã¹ãã¼ãããèªã¿ãã ããã
- Django 4.0 ãªãªã¼ã¹ã®æ¦è¦
- Redisã®ãã£ãã·ã¥ããã¯ã¨ã³ããçµè¾¼ã¿ã§ãµãã¼ã
- zoneinfoãããã©ã«ãã®ã¿ã¤ã ã¾ã¼ã³å®è£ ã«
- ãã©ã¼ã ã¯ã©ã¹ã«ãã³ãã¬ã¼ããæå®ããå±æ§ã追å
- ã¾ã¨ã
Django 4.0 ãªãªã¼ã¹ã®æ¦è¦
å称 | Django |
---|---|
ãªãªã¼ã¹ãã¼ã¸ã§ã³ | Django 4.0 |
ãªãªã¼ã¹æ¥ | 2021å¹´12æ7æ¥ |
ãµãã¼ããã¦ããPython | 3.8ã3.9ã3.10 |
ãªãªã¼ã¹ãã¼ã | https://docs.djangoproject.com/en/4.0/releases/4.0/ |
â»ã¢ãããã¼ãã®éã¯Backwards Incompatible Changesï¼å¾æ¹äºææ§ã®ãªãå¤æ´ï¼ã«åå注æãã¦ãã ãã
Redisã®ãã£ãã·ã¥ããã¯ã¨ã³ããçµè¾¼ã¿ã§ãµãã¼ã
ããã¾ã§ãDjangoã®ãã£ãã·ã¥ããã¯ã¨ã³ãã¨ãã¦Redisã使ç¨ããã«ã¯ããµã¼ããã¼ãã£ã¼è£½ã® django-redis ã使ãå¿ è¦ãããã¾ããã
Django 4.0ããã¯ãRedisã®ãã£ãã·ã¥ããã¯ã¨ã³ããçµè¾¼ã¿ã§ãµãã¼ãããã¾ãã
ããã使ãã«ã¯ãRedisã®Pythonãã¤ã³ãã£ã³ã°ã§ãã redis-py ãå¿
è¦ã§ãã
ï¼ django-redis ã®å ´åã redis-py ã¯ä¾åããã±ã¼ã¸ã¨ãã¦ã¤ã³ã¹ãã¼ã«ããã¾ãï¼
åºæ¬çãªãã£ãã·ã¥æ©è½ã§ããã°ãçµè¾¼ã¿ããã¯ã¨ã³ãã使ãã°ããã§ãããã
ãã ããçµè¾¼ã¿ããã¯ã¨ã³ãã§å¯¾å¿ãã¦ããªãRedisã®é«åº¦ãªæ©è½ã使ç¨ãããå ´åã¯ã django-redis ã使ãå¿
è¦ãããã¾ãã
詳細㯠https://github.com/jazzband/django-redis/issues/585#issuecomment-1002051473 ãã覧ãã ããã
zoneinfoãããã©ã«ãã®ã¿ã¤ã ã¾ã¼ã³å®è£ ã«
Djangoã«ã¯ã¿ã¤ã ã¾ã¼ã³å®è£ ã pytz ãã zoneinfo ã¸ç§»è¡ããè¨ç»ãããã¾ãã
ãã®è¨ç»ã®ä¸ç°ã¨ãã¦ãã¾ãDjango 3.2㧠zoneinfo ããµãã¼ãããã¾ããã
ï¼ãã ããã¾ã ããã©ã«ã㯠pytz ã§ããï¼
ãããä»åã®Django 4.0ããã¯ãzoneinfo ãããã©ã«ãã®ã¿ã¤ã ã¾ã¼ã³å®è£ ã«ãªãã pytz ã¯éæ¨å¥¨ã¨ãªãã¾ããã
ããã¦ãDjango 5.0ãã pytz ã¯Djangoã®ãµãã¼ã対象å¤ã«ãªãã¾ãã
zoneinfo ã¯Python 3.9ãã追å ãããã¢ã¸ã¥ã¼ã«ã§ãã
ãã®ãããPython 3.8ã§Django 4.0ãã¤ã³ã¹ãã¼ã«ããã¨ã zoneinfo ã®ä»£ããã¨ã㦠backports.zoneinfo ãèªåçã«ã¤ã³ã¹ãã¼ã«ããã¾ãã
ãªãã pytz 㨠zoneinfo ã«ã¯å®å
¨ã«äºææ§ãããããã§ã¯ããã¾ããã
ã¿ã¤ã ã¾ã¼ã³è¨å®ãUTC以å¤ã§ pytz ã®normalize()
ã localize()
ã使ã£ã¦ããå ´åã¯ãåå¥ã«ç¢ºèªãã¦å¯¾å¿ãå¿
è¦ã§ãã
pytzããzoneinfoã¸ã®ç§»è¡æ¹æ³
移è¡æ¹æ³ã¨ãã¦ã¯ã主ã«2éãèãããã¾ãã
1ã¤ç®ã¯ãDjango 3.2 ã®ã¾ã¾ã¿ã¤ã ã¾ã¼ã³å®è£
ã zoneinfo ã«ç§»è¡ãããã®å¾Django 4.0ã«ã¢ããã°ã¬ã¼ãããæ¹æ³ã§ãã
ã¾ã Django 3.2ã使ãç¶ããã®ã§ããã°ãå
è¡ãã¦å¯¾å¿ãã¦ãããã¨ã§ãå°æ¥ã®ä½æ¥ãæ¸ããã¾ãã
2ã¤ç®ã¯ãã¾ãDjango 4.0ã«ã¢ããã°ã¬ã¼ããã¦ããã®å¾zoneinfoã«ç§»è¡ããæ¹æ³ã§ãã
æ¥ãã§Django 4.0ã«ã¢ããã°ã¬ã¼ãããå¿
è¦ãããå ´åã¯ããã¡ãã®æ¹æ³ã«ãªãã§ãããã
ãªãã移è¡ã®ããã®æªç½®ã¨ãã¦ã USE_DEPRECATED_PYTZ ãç¨æããã¦ãã¾ãã
settings.py
ã« USE_DEPRECATED_PYTZ = True
ãè¨å®ããã°ãå¼ãç¶ã pytz ã使ãã¾ãã
ãã ãã USE_DEPRECATED_PYTZ ã¯Django 5.0ã§åé¤ããã¾ãã
移è¡ã¢ã·ã¹ãããã±ã¼ã¸ pytz deprecation_shim package
ã¾ãã移è¡ãã¢ã·ã¹ãããããã±ã¼ã¸ã¨ãã¦ãpytz_deprecation_shim package ãåå¨ãã¾ãã
pytz ãã zoneinfo ã¸ã®ç§»è¡ã§ã¯ãå¿ è¦ã«å¿ã㦠USE_DEPRECATED_PYTZ 㨠pytz_deprecation_shim packageãå©ç¨ããã¨ããã§ãããã
ãã©ã¼ã ã¯ã©ã¹ã«ãã³ãã¬ã¼ããæå®ããå±æ§ã追å
ãã©ã¼ã é¢é£ã®ã¯ã©ã¹ï¼Forms ã Formsets ã ErrorListï¼ã«ãtemplate_name
å±æ§ã追å ããã¾ããã
Djangoã§ã¯ãDjangoãã³ãã¬ã¼ãã«ãã©ã¼ã ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã渡ãããã³ãã¬ã¼ãå
㧠{{ form }}
ã®ããã«è¨è¿°ããã°ãã©ã¼ã ãèªåçæãã¦ããã¾ãã
ããããèªåçæããããã©ã¼ã ã®è¡¨ç¤ºãè¦ä»¶ã«åããªãå ´åãDjangoãã³ãã¬ã¼ãã§ãã©ã¼ã ãã«ã¹ã¿ãã¤ãºããªããã°ãªãã¾ããã
Django 4.0ã§è¿½å ãããtemplate_name
å±æ§ã«ãã³ãã¬ã¼ããè¨å®ããã¨ã{{ form }}
ã¨è¨è¿°ããç®æã«ãæå®ãããã³ãã¬ã¼ãã使ã£ã¦ãã©ã¼ã ã表示ããã¾ãã
ç°¡åãªä¾ã使ã£ã¦èª¬æãã¾ãããã
以ä¸ã¯ã·ã³ãã«ãªãã©ã¼ã ã¯ã©ã¹ã¨ãããã表示ããDjangoãã³ãã¬ã¼ãããã³ãã¥ã¼é¢æ°ã§ãã
ï¼importæãªã©ã¯çç¥ãã¦ãã¾ãï¼
ãªããæ¯è¼ã®ããã«ãã¾ã ãã©ã¼ã ã¯ã©ã¹ã® template_name
å±æ§ã¯ä½¿ç¨ãã¦ãã¾ããã
forms.py
class MyForm(forms.Form): message = forms.CharField(label='Message', max_length=100) name = forms.CharField(label='Name', max_length=50)
index.html
<form action="{% url 'myapp:send' %}" method="post"> {{ form }} <input type="submit" value="OK"> </form>
views.py
def index(request): form = MyForm() return render(request, "index.html", {"form": form})
ããã¯ã以ä¸ã®ãããªç»é¢ã«ãªãã¾ããDjangoã«ããããã©ã¼ã ãèªåçæããã¦ãã¾ãã
次ã«ããã©ã¼ã ç¨ã®ãã³ãã¬ã¼ã form_snippet.html
ãä½æãããããMyForm
ã®template
å±æ§ã«è¨å®ãã¦ã¿ã¾ãã
form_snippet.html
{% for field in form %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }} {{ field }} </div> {% endfor %}
forms.py
class MyForm(forms.Form): template_name = "form_snippet.html" # 追å message = forms.CharField(label='Message', max_length=100) name = forms.CharField(label='Name', max_length=50)
ããã¯ã以ä¸ã®ãããªç»é¢ã«ãªãã¾ããtemplate_name
ãè¨å®ãããã¨ã§ããã©ã¼ã ã®è¡¨ç¤ºãå¤ããã¾ããã
ãªãã template_name
ã使ããªãã¦ãã以ä¸ã®ããã«{% include %}
ã使ãã°åãããã«è¡¨ç¤ºããã¾ãã
index.html
<form action="{% url 'myapp:send' %}" method="post"> {% include "form_snippet.html" %} {# 追å #} <input type="submit" value="OK"> </form>
ãã®ãããããã¾ã§ã¨ãã¾ãå¤ãããªãããã«æããããããã¾ããããtemplate_name
ã使ãã¨ä»¥ä¸ã®ã¡ãªãããããã¨èãã¾ãã
template_name
å±æ§ãæå®ãããã¨ã§ããã©ã¼ã ã¯ã©ã¹ã¨ãã©ã¼ã ç¨ã®ãã³ãã¬ã¼ãã®å¯¾å¿ãåããããããªã{% include xxx %}
ã¨æ¸ããã{{ form }}
ã¨æ¸ãæ¹ããã©ã¼ã ãã¬ã³ããªã³ã°ãããã¨ãæ確ã«ã§ãã
ããã¾ã§ããã³ãã¬ã¼ãã使ã£ããã©ã¼ã ã®ã«ã¹ã¿ãã¤ãºã¯ã§ãã¾ããããtemplate_name
å±æ§ã使ãã¨ãããã¹ãã¼ããªæ¸ãæ¹ãã§ãããã§ããã
ã¾ã¨ã
Django 4.0ã§ã¯ãç®ç«ã£ãæ©è½è¿½å ã¯ããã¾ããããç´°ããã¨ããã§ä½¿ãããããåä¸ãã¦ããã¨æãã¾ãã
ã¾ããpytz ã¯Django 5.0ã§å»æ¢ããã¾ããpytz ãã zoneinfo ã¸ã®åæ¿ãDjango 5.0 ã¸ã®ç§»è¡éå£ã¨ãªããªããããä»ã®ãã¡ã«ç§»è¡è¨ç»ãç«ã¦ã¦ããã¾ãããã