{"id":10435,"date":"2020-09-10T22:03:49","date_gmt":"2020-09-10T13:03:49","guid":{"rendered":"http:\/\/www.webcyou.com\/?p=10435"},"modified":"2020-09-10T22:03:49","modified_gmt":"2020-09-10T13:03:49","slug":"django-drf%e3%81%a7%e3%80%81%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e3%81%ae%e3%83%a2%e3%83%87%e3%83%ab%e3%82%92%e4%bd%9c%e6%88%90%e6%99%82%e3%81%ab%e3%83%8f%e3%83%83%e3%82%b7%e3%83%a5id%e3%82%92","status":"publish","type":"post","link":"https:\/\/www.webcyou.com\/?p=10435","title":{"rendered":"Django (DRF)\u3067\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u6642\u306b\u30cf\u30c3\u30b7\u30e5id\u3092\u5225\u3067\u4fdd\u5b58\u3059\u308b\u3002"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10252\" src=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2020\/04\/d17fb3e60f0c8b6b4cd0c00a57927e28.png\" alt=\"\" width=\"319\" height=\"62\" srcset=\"https:\/\/www.webcyou.com\/wp-content\/uploads\/2020\/04\/d17fb3e60f0c8b6b4cd0c00a57927e28.png 319w, https:\/\/www.webcyou.com\/wp-content\/uploads\/2020\/04\/d17fb3e60f0c8b6b4cd0c00a57927e28-300x58.png 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/p>\n<p>\u3069\u3082\u3067\u3059\u3002<\/p>\n<p>Django\u6271\u3063\u3066\u3044\u308b\u3068\u3001\u300c\u3042\u308c\u3001\u3053\u308c\u3069\u3046\u3084\u308b\u3093\u3060\u3063\u3051\u300d\u3068\u601d\u3063\u3066\u30cd\u30c3\u30c8\u3067\u8abf\u3079\u305f\u308a\u3059\u308b\u306e\u3067\u3059\u304c\u3001<\/p>\n<p>\u3069\u3046\u3082\u3001 Django (DRF)\u306e\u60c5\u5831\u304c\u30cd\u30c3\u30c8\u4e0a\u306b\u5c11\u306a\u3044\u5370\u8c61\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u30cf\u30de\u3063\u305f\u306e\u304c\u3001\u8868\u984c\u3067\u3042\u308b\u3088\u3046\u306b\u3001\u300c<strong>\u30e6\u30fc\u30b6\u30fc\u306e\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u6642\u306e\u4e00\u5ea6\u3060\u3051\uff08\u6700\u521d\uff09\u306b\u30cf\u30c3\u30b7\u30e5id\u3092\u5225\u3067\u4fdd\u5b58\u3059\u308b<\/strong>\u300d\u3068\u8a00\u3046\u3053\u3068\u3092\u3084\u308a\u305f\u3044\u304c\u3001\u306f\u3066\u3069\u3046\u3084\u308b\u306e\u3060\u308d\u3046\u3068\u3002<\/p>\n<p>\u3088\u304f\u30d2\u30c3\u30c8\u3057\u305f\u3084\u308a\u65b9\u304c\u3053\u306e\u69d8\u306a\u611f\u3058\u3068\u304b\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class CustomUser(AbstractUser):\r\n    hashid = HashidField(null=True, blank=True)\r\n\r\n    def save(self, *args, **kw):\r\n        self.hashid = self.id\r\n        return super(CustomUser, self).save(*args, **kw)<\/pre>\n<p>\u3068\u304b\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class Article(models.Model):\r\n    hashid = models.CharField(...)\r\n\r\n    def save(self, *args, **kwargs):\r\n        super(Article, self).save(*args, **kwargs)\r\n        self.hashid = hashids.encode(self.id)\r\n        super(Article, self).save(*args, **kwargs)<\/pre>\n<p>\u3060\u304c\u3001<\/p>\n<p>\u3069\u3046\u3082\u4e0a\u624b\u304f\u3044\u304b\u306a\u3044\u3002<\/p>\n<p>\u305d\u3082\u305d\u3082\u3001\u30cf\u30c3\u30b7\u30e5\u5316\u3059\u308b\u70ba\u306b\u3001<a href=\"https:\/\/github.com\/nshafer\/django-hashid-field\" target=\"_blank\" rel=\"noopener\">django-hashid-field<\/a>\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>\u3053\u3084\u3064\u3001\u306a\u304b\u306a\u304b\u826f\u3044\u611f\u3058\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001\u901a\u5e38\u306a\u3089\u3053\u306e\u69d8\u306a\u5f62\u3067id\u3092\u30cf\u30c3\u30b7\u30e5\u5316\u3057\u305f\u308a\u3059\u308b\u306e\u3067\u3059\u304c\u3001<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class Author(models.Model):\r\n    id = HashidAutoField(primary_key=True)\r\n    name = models.CharField(max_length=40)<\/pre>\n<p>\u5f53\u65b9\u3067\u306f\u3001<a href=\"https:\/\/django-allauth.readthedocs.io\/en\/latest\/installation.html\" target=\"_blank\" rel=\"noopener\">django-allauth<\/a>(django rest auth)\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u306e\u3067\u3001id(pk)\u3092\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u3059\u308b\u69d8\u306a\u5f62\u3060\u3068\u30e9\u30a4\u30d6\u30e9\u30ea\u5074\u3067\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u3084\u3084\u3053\u3057\u3044\u3002<\/p>\n<p>\u3042\u3068loop\u306b\u30cf\u30de\u3063\u305f\u308a\u3057\u3066\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3002\u3069\u3046\u3057\u305f\u3082\u306e\u304b\u3002<\/p>\n<p>\u3067\u3001\u6700\u7d42\u7684\u306b\u843d\u3061\u7740\u3044\u305f\u306e\u304c\u3001\u3053\u306e\u3084\u308a\u65b9\u3002<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">class CustomUser(AbstractUser):\r\n    hashid = HashidField(null=True, blank=True)\r\n\r\n@receiver(post_save, sender=CustomUser)\r\ndef hashid_update(sender, instance, created, **kwargs):\r\n    if created:\r\n        instance.hashid = instance.id\r\n        instance.save()<\/pre>\n<p>receiver\u306e\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u5229\u7528\u3057\u3066\u3001\u65b0\u898f\u4f5c\u6210\u306epost\u6642\u306b\u4e00\u5ea6\u3060\u3051\u5b9f\u884c\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u305f\u3002<\/p>\n<p>\u8272\u3005\u3068\u3001\u63a2\u3059\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u30e1\u30e2\u7a0b\u5ea6\u306b\u3068\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3082\u3067\u3059\u3002 Django\u6271\u3063\u3066\u3044\u308b\u3068\u3001\u300c\u3042\u308c\u3001\u3053\u308c\u3069\u3046\u3084\u308b\u3093\u3060\u3063\u3051\u300d\u3068\u601d\u3063\u3066\u30cd\u30c3\u30c8\u3067\u8abf\u3079\u305f\u308a\u3059\u308b\u306e\u3067\u3059\u304c\u3001 \u3069\u3046\u3082\u3001 Django (DRF)\u306e\u60c5\u5831\u304c\u30cd\u30c3\u30c8\u4e0a\u306b\u5c11\u306a\u3044\u5370\u8c61\u3002 \u3061\u3087\u3063\u3068\u30cf\u30de\u3063\u305f\u306e\u304c\u3001\u8868\u984c\u3067\u3042\u308b\u3088\u3046\u306b\u3001\u300c\u30e6\u30fc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[257],"tags":[177,168,176,37,44,45,178,78,118],"class_list":{"0":"post-10435","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-python","7":"tag-css3","8":"tag-html","9":"tag-html5","10":"tag-iphone-","11":"tag-web","13":"tag-178","14":"tag-78","15":"tag-118"},"_links":{"self":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/10435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10435"}],"version-history":[{"count":1,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/10435\/revisions"}],"predecessor-version":[{"id":10436,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=\/wp\/v2\/posts\/10435\/revisions\/10436"}],"wp:attachment":[{"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcyou.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}