\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u306b\u8a18\u8f09\u304c\u3042\u308b\u3088\u3046\u306b, 2\u3064\u306e\u5f15\u6570\u3092\u3068\u308a, \u30d5\u30a1\u30a4\u30eb\u3078\u306e\u30d1\u30b9\u3092\u8fd4\u3059\u95a2\u6570\u3092\u5b9a\u7fa9\u3057, \u3053\u308c\u3092upload_to\u306b\u4e0e\u3048\u308b\u3053\u3068\u3067, \u4efb\u610f\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3067\u4fdd\u5b58\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059. \u4f7f\u7528\u4f8b\u3092\u4ee5\u4e0b\u306b\u793a\u3057\u307e\u3059.<\/p>\n<\/p>\n
\r\nimport hashlib\r\nimport os.path\r\nfrom django.db import models\r\n\r\ndef get_image_path(instance, filename):\r\n return "images\/%s%s" % (hashlib.sha1(instance.title + filename).hexdigest(), os.path.splitext(filename)[1])\r\n\r\nclass Sample(models.Model):\r\n title = models.CharField(max_length=256)\r\n image = models.ImageField(upload_to=get_image_path)\r\n<\/pre>\nget_image_path\u306finstance\u3068filename\u306e2\u3064\u306e\u5f15\u6570\u3092\u53d6\u308a\u307e\u3059. instance\u306b\u306fFileField\u3084ImageField\u304c\u5b9a\u7fa9\u3055\u308c\u305f\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u4e0e\u3048\u3089\u308c\u307e\u3059.filename\u306b\u306f\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u304c\u4e0e\u3048\u3089\u308c\u307e\u3059. \u3053\u3053\u3067\u306f, \u30e2\u30c7\u30eb\u306etitle\u30d5\u30a3\u30fc\u30eb\u30c9\u3068\u30d5\u30a1\u30a4\u30eb\u540d\u304b\u3089\u30cf\u30c3\u30b7\u30e5\u5024\u3092\u6c42\u3081, \u62e1\u5f35\u5b50\u3092\u4ed8\u52a0\u3057\u305f\u3082\u306e\u3092\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u5148\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u3068\u3057\u3066\u3044\u307e\u3059. get_image_path\u306fMEDIA_ROOT\u304b\u3089\u306e\u76f8\u5bfe\u30d1\u30b9\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u307e\u3059.<\/p>\n","protected":false},"excerpt":{"rendered":"
Django\u306e\u30e2\u30c7\u30eb\u3067FileField\u3084ImageField\u3092\u5229\u7528\u3057\u305f\u5834\u5408, upload_to\u3067\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u4fdd\u5b58\u3059\u308b\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u5909\u66f4\u3067\u304d\u307e\u3059\u304c, upload_to\u306b\u95a2\u6570\u3092\u6e21\u3059\u3053\u3068\u306b\u3088\u3063\u3066\u4fdd\u5b58\u3059\u308b\u30d5 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[12,15],"_links":{"self":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/159"}],"collection":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":5,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/posts\/159\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ymyzk.com\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}