{"id":4760,"date":"2023-09-29T17:02:56","date_gmt":"2023-09-29T08:02:56","guid":{"rendered":"https:\/\/417.run\/?p=4760"},"modified":"2023-11-22T19:26:32","modified_gmt":"2023-11-22T10:26:32","slug":"custom-order","status":"publish","type":"post","link":"https:\/\/417.run\/pg\/python\/django\/custom-order\/","title":{"rendered":"Django\u3067\u7279\u5b9a\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u512a\u5148\u3057\u3066\u30bd\u30fc\u30c8\u3059\u308b"},"content":{"rendered":"<a class=\"c_linkto longc_linkto\" href=\"https:\/\/417.run\/pg\/python\/django\/sort-case-when\/\">\n  <span class=\"longc_img\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/417.run\/wp-content\/themes\/sango-theme\/library\/images\/default_small.jpg\" alt=\"\u3010Django\u3011\u6587\u5b57\u5217\u3067\u597d\u304d\u306a\u9806\u756a\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3010Case, When\u3011\" width=\"520\" height=\"300\" ><\/span>\n  <span class=\"longc_content c_linkto_text\"><time class=\"pubdate sng-link-time dfont\" itemprop=\"datePublished\" datetime=\"2023-11-22\">2023\u5e7411\u670822\u65e5<\/time><span class=\"longc_title\">\u3010Django\u3011\u6587\u5b57\u5217\u3067\u597d\u304d\u306a\u9806\u756a\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3010Case, When\u3011<\/span><\/span>\n<\/a>\n\u7279\u5b9a\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u6307\u5b9a\u3057\u3066\u3001\u305d\u308c\u3089\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u30bd\u30fc\u30c8\u9806\u3092\u4e0a\u306b\u3059\u308b\u5834\u5408\u3001<br \/>\nDjango\u306eORM\u306e<code>annotate()<\/code>\u3068<code>Case()<\/code>\u3092\u4f7f\u3046\u3053\u3068\u3067\u3053\u308c\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4f8b\u3068\u3057\u3066\u3001<code>Item<\/code>\u30e2\u30c7\u30eb\u304c\u3042\u308a\u3001<code>id<\/code>\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u57fa\u3065\u3044\u3066\u7279\u5b9a\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u4e0a\u306b\u30bd\u30fc\u30c8\u3057\u305f\u3044\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u65b9\u6cd5\u3067\u3053\u308c\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059:<\/p>\n<pre><code class=\"language-python\">from django.db.models import Case, When, Value, IntegerField\n\n# \u512a\u5148\u7684\u306b\u30bd\u30fc\u30c8\u3057\u305f\u3044\u30a2\u30a4\u30c6\u30e0\u306eID\u30ea\u30b9\u30c8\npriority_ids = [3, 1, 2]\n\n# \u30a2\u30a4\u30c6\u30e0\u3092\u6307\u5b9a\u306e\u9806\u756a\u3067\u30bd\u30fc\u30c8\nitems = Item.objects.annotate(\n    custom_order=Case(\n        *[When(id=id, then=Value(idx)) for idx, id in enumerate(priority_ids)],\n        default=Value(len(priority_ids)),\n        output_field=IntegerField()\n    )\n).order_by(&#039;custom_order&#039;)\n\nfor item in items:\n    print(item.id)<\/code><\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u3001<code>priority_ids<\/code>\u306e\u30ea\u30b9\u30c8\u5185\u3067\u306e\u9806\u5e8f\u306b\u57fa\u3065\u3044\u3066\u3001<code>Item<\/code>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u30bd\u30fc\u30c8\u3057\u307e\u3059\u3002<br \/>\n\u30ea\u30b9\u30c8\u306b\u542b\u307e\u308c\u306a\u3044\u30a2\u30a4\u30c6\u30e0\u306f\u3001\u30ea\u30b9\u30c8\u306e\u5f8c\u308d\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002<br \/>\n\u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u3046\u3068\u3001\u7279\u5b9a\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u30bd\u30fc\u30c8\u9806\u3092\u4e0a\u306b\u6301\u3063\u3066\u304f\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<a class=\"c_linkto longc_linkto\" href=\"https:\/\/417.run\/pg\/python\/django\/sort-case-when\/\">\n  <span class=\"longc_img\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/417.run\/wp-content\/themes\/sango-theme\/library\/images\/default_small.jpg\" alt=\"\u3010Django\u3011\u6587\u5b57\u5217\u3067\u597d\u304d\u306a\u9806\u756a\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3010Case, When\u3011\" width=\"520\" height=\"300\" ><\/span>\n  <span class=\"longc_content c_linkto_text\"><time class=\"pubdate sng-link-time dfont\" itemprop=\"datePublished\" datetime=\"2023-11-22\">2023\u5e7411\u670822\u65e5<\/time><span class=\"longc_title\">\u3010Django\u3011\u6587\u5b57\u5217\u3067\u597d\u304d\u306a\u9806\u756a\u306b\u30bd\u30fc\u30c8\u3059\u308b\u3010Case, When\u3011<\/span><\/span>\n<\/a>\n","protected":false},"excerpt":{"rendered":"<p>\u7279\u5b9a\u306e\u30a2\u30a4\u30c6\u30e0\u3092\u6307\u5b9a\u3057\u3066\u3001\u305d\u308c\u3089\u306e\u30a2\u30a4\u30c6\u30e0\u306e\u30bd\u30fc\u30c8\u9806\u3092\u4e0a\u306b\u3059\u308b\u5834\u5408\u3001 Django\u306eORM\u306eannotate()\u3068Case()\u3092\u4f7f\u3046\u3053\u3068\u3067\u3053\u308c\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002 \u4f8b\u3068\u3057\u3066\u3001Item\u30e2\u30c7\u30eb\u304c\u3042\u308a\u3001id\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u57fa\u3065\u3044\u3066\u7279\u5b9a &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"class_list":{"0":"post-4760","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-django","7":"nothumb"},"_links":{"self":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4760"}],"collection":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/comments?post=4760"}],"version-history":[{"count":2,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4760\/revisions"}],"predecessor-version":[{"id":4920,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4760\/revisions\/4920"}],"wp:attachment":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/media?parent=4760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/categories?post=4760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/tags?post=4760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}