{"id":6012,"date":"2020-12-21T23:39:45","date_gmt":"2020-12-21T14:39:45","guid":{"rendered":"http:\/\/blog.jansnap.com\/?p=6012"},"modified":"2021-12-19T20:35:09","modified_gmt":"2021-12-19T11:35:09","slug":"django%e3%81%a7%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%9ffield%e3%81%a0%e3%81%91%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b%e3%82%af%e3%83%a9%e3%82%b9%e3%83%99%e3%83%bc%e3%82%b9%e6%b1%8e%e7%94%a8%e3%83%93%e3%83%a5","status":"publish","type":"post","link":"https:\/\/blog.jansnap.com\/?p=6012","title":{"rendered":"Django\u3067\u6307\u5b9a\u3057\u305ffield\u3060\u3051\u8868\u793a\u3059\u308b(\u30af\u30e9\u30b9\u30d9\u30fc\u30b9\u6c4e\u7528\u30d3\u30e5\u30fc\u306eDetailView\u3092\u4f7f\u3046)"},"content":{"rendered":"\r\n<h2><span class=\"ez-toc-section\" id=\"%E6%A6%82%E8%A6%81\"><\/span>\r\n    \u6982\u8981<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n<ul>\r\n    <li>Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u3001\u3042\u3089\u304b\u3058\u3081\u6307\u5b9a\u3057\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u3060\u3051\u8868\u793a\u3057\u305f\u3044\r\n\r\n        <ul>\r\n            <li>User\u30e2\u30c7\u30eb\u3067\u3001is_active\u306a\u3069\u4e0d\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u898b\u305b\u305f\u304f\u306a\u3044<\/li>\r\n            <li>\r\n                <code>django.views.generic.DetailView<\/code> \u3067\u8868\u793a\u3059\u308b\u60f3\u5b9a<\/li>\r\n        <\/ul>\r\n    <\/li>\r\n    <li>\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5f8c\u304b\u3089\u5897\u6e1b\u3055\u305b\u305f\u3068\u304d\u306b\u3001\u306a\u308b\u3079\u304f\u4e00\u7b87\u6240\u3060\u3051\u306e\u5909\u66f4\u3067\u6e08\u307e\u305b\u305f\u3044<\/li>\r\n<\/ul>\r\n\r\n<h2><span class=\"ez-toc-section\" id=\"%E5%AF%BE%E5%BF%9C\"><\/span>\r\n    \u5bfe\u5fdc<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n<ul>\r\n    <li>model\u30af\u30e9\u30b9\u306b\u3001\u6307\u5b9a\u3057\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u3060\u3051\u3092\u62bd\u51fa\u3059\u308b\u95a2\u6570\u3092\u8ffd\u52a0\u3059\u308b\r\n\r\n        <ul>\r\n            <li>view\u3054\u3068\u306b\u8868\u793a\u3059\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5909\u3048\u305f\u3044\u5834\u5408\u306f\u3001\u4e0b\u8a18\u306e<code>viewable_field_list<\/code>\u3092\u4e0a\u66f8\u304d\u3059\u308b<\/li>\r\n        <\/ul>\r\n    <\/li>\r\n<\/ul>\r\n\r\n<h2><span class=\"ez-toc-section\" id=\"%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89\"><\/span>\r\n    \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n<h3><span class=\"ez-toc-section\" id=\"model%EF%BD%93py\"><\/span>\r\n    model\uff53.py<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class CustomUser(AbstractUser):\r\n\r\n    # get viewable fields with order.\r\n    viewable_field_list = ('userType', 'username', 'email', 'companyName', 'nearestStation')\r\n    editable_field_list = ('username', 'companyName', 'nearestStation')\r\n    def get_selected_fields(self):\r\n        \"\"\"Returns a list of selected field names on the instance.\"\"\"\r\n        fields = []\r\n        for vf in self.viewable_field_list:\r\n            for f in self._meta.fields:\r\n                fname = f.name\r\n                if vf != fname:\r\n                    continue\r\n                # resolve picklists\/choices, with get_xyz_display() function\r\n                get_choice = 'get_'+fname+'_display'\r\n                if hasattr(self, get_choice):\r\n                    value = getattr(self, get_choice)()\r\n                else:\r\n                    try:\r\n                        value = getattr(self, fname)\r\n                    except AttributeError:\r\n                        value = None\r\n\r\n                # only display fields with values and skip some fields entirely\r\n                if f.editable:  # and value:\r\n                    fields.append(\r\n                    {\r\n                    'label':f.verbose_name,\r\n                    'name':f.name,\r\n                    'value':value,\r\n                    'is_editable': f.name in self.editable_field_list\r\n                    }\r\n                    )\r\n        return fields<\/code><\/pre>\r\n\r\n\r\n\r\n<h3><span class=\"ez-toc-section\" id=\"template\"><\/span>\r\n    template<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code lang=\"html\" class=\"language-html\"><ul>\r\n  {% for f in object.get_selected_fields %}\r\n    <li>{{ f.label|capfirst }} ( {{ f.name }} ) : {{ f.value|escape|urlize }} (editable={{ f.is_editable }})<\/li>\r\n  {% endfor %}\r\n<\/ul><\/code><\/pre>\r\n\r\n\r\n\r\n<h2><span class=\"ez-toc-section\" id=\"%E5%BF%85%E8%A6%81%E3%81%AB%E5%BF%9C%E3%81%98%E3%81%A6%E3%80%81view%E3%81%AE%E8%A8%98%E8%BF%B0%E3%82%82%E5%A4%89%E6%9B%B4\"><\/span>\r\n    \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u3001view\u306e\u8a18\u8ff0\u3082\u5909\u66f4<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n<h3><span class=\"ez-toc-section\" id=\"viewspy\"><\/span>\r\n    views.py<span class=\"ez-toc-section-end\"><\/span><\/h3>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class UserDetailView(OnlyYouMixin, DetailView):\r\n    model = CustomUser\r\n\r\n    def get_context_data(self, **kwargs):\r\n        context = super().get_context_data(**kwargs)\r\n        if int(context[\"object\"].userType.id) == USERTYPE_SUPPLIER:\r\n            self.model.viewable_field_list = ('userType', 'username', 'email', 'companyName',)\r\n        elif int(context[\"object\"].userType.id) == USERTYPE_BUYER:\r\n            self.model.viewable_field_list = ('userType', 'username', 'email', 'nearestStation')\r\n        return context<\/code><\/pre>\r\n\r\n\r\n\r\n<h2><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83\"><\/span>\r\n    \u53c2\u8003<span class=\"ez-toc-section-end\"><\/span><\/h2>\r\n\r\n<ul>\r\n    <li><a href=\"https:\/\/stackoverflow.com\/questions\/2170228\/iterate-over-model-instance-field-names-and-values-in-template\/3431104#3431104\" rel=\"nofollow noopener\" target=\"_blank\">Iterate over model instance field names and values in template<\/a><\/li>\r\n<\/ul>\r\n","protected":false},"excerpt":{"rendered":"<ul>\n<li>Django\u306e\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3067\u3001\u3042\u3089\u304b\u3058\u3081\u6307\u5b9a\u3057\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u3060\u3051\u8868\u793a\u3057\u305f\u3044\n<ul>\n<li>User\u30e2\u30c7\u30eb\u3067\u3001is_active\u306a\u3069\u4e0d\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u898b\u305b\u305f\u304f\u306a\u3044<\/li>\n<li>\n<code>django.views.generic.DetailView<\/code> \u3067\u8868\u793a\u3059\u308b\u60f3\u5b9a<\/li>\n<\/ul>\n<\/li>\n<li>\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u5f8c\u304b\u3089\u5897\u6e1b\u3055\u305b\u305f\u3068\u304d\u306b\u3001\u306a\u308b\u3079\u304f\u4e00\u7b87\u6240\u3060\u3051\u306e\u5909\u66f4\u3067\u6e08\u307e\u305b\u305f\u3044<\/li>\n<\/ul>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4106,4108,4070],"tags":[4103,4105],"class_list":["post-6012","post","type-post","status-publish","format-standard","hentry","category-django","category-django30","category-python","tag-django","tag-django30"],"_links":{"self":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6012"}],"version-history":[{"count":3,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6012\/revisions"}],"predecessor-version":[{"id":6372,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=\/wp\/v2\/posts\/6012\/revisions\/6372"}],"wp:attachment":[{"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jansnap.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}