{"action":"created","issue":{"url":"https://api.github.com/repos/viewflow/django-material/issues/402","repository_url":"https://api.github.com/repos/viewflow/django-material","labels_url":"https://api.github.com/repos/viewflow/django-material/issues/402/labels{/name}","comments_url":"https://api.github.com/repos/viewflow/django-material/issues/402/comments","events_url":"https://api.github.com/repos/viewflow/django-material/issues/402/events","html_url":"https://github.com/viewflow/django-material/issues/402","id":301326118,"number":402,"title":"How to override widget template ?","user":{"login":"jpic","id":94636,"avatar_url":"https://avatars3.githubusercontent.com/u/94636?v=4","gravatar_id":"","url":"https://api.github.com/users/jpic","html_url":"https://github.com/jpic","followers_url":"https://api.github.com/users/jpic/followers","following_url":"https://api.github.com/users/jpic/following{/other_user}","gists_url":"https://api.github.com/users/jpic/gists{/gist_id}","starred_url":"https://api.github.com/users/jpic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpic/subscriptions","organizations_url":"https://api.github.com/users/jpic/orgs","repos_url":"https://api.github.com/users/jpic/repos","events_url":"https://api.github.com/users/jpic/events{/privacy}","received_events_url":"https://api.github.com/users/jpic/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-01T09:04:45Z","updated_at":"2018-03-01T09:11:16Z","closed_at":"2018-03-01T09:11:16Z","author_association":"NONE","body":"Hello !\r\n\r\nFirst, gratz for making the best form lib for Django ;)\r\n\r\nCurrently, for select fields it uses django_select.html, I'm trying to override it.\r\n\r\nI need to add an option to a ModelChoiceField's select, because it's an option that should **not** validate, and only be used by JS.\r\n\r\nInitially I went for this:\r\n\r\n```\r\nclass CPAMSelect(forms.Select):\r\n    template_name = 'cpam/select.html'\r\n\r\nclass CPAMChoiceField(forms.ModelChoiceField):\r\n    def __init__(self, **kwargs):\r\n        kwargs['widget'] = CPAMSelect\r\n        super().__init__(**kwargs)\r\n```\r\n\r\nWith a naive template:\r\n\r\n```\r\n<select name=\"{{ widget.name }}\"{% include \"django/forms/widgets/attrs.html\" %}>{% for\r\n group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}\r\n  <optgroup label=\"{{ group_name }}\">{% endif %}{% for option in group_choices %}\r\n  {% include option.template_name with widget=option %}{% endfor %}{% if group_name %}\r\n  </optgroup>{% endif %}{% endfor %}\r\n  <option value=\"other\">Autre</option>\r\n</select>\r\n```\r\n\r\nWhich looked like it would work:\r\n\r\n```\r\ndef test_cpamchoicefield():\r\n    cpam = CPAM.objects.create(\r\n        code='123123123',\r\n        label='test cpam',\r\n        liquidation_email='liquidation@example.com',\r\n    )\r\n    field = CPAMChoiceField()\r\n    result = field.widget.render('test', cpam.pk)\r\n    assert result == '''<select name=\"test\">\r\n  <option value=\"\">Merci de choisir votre CPAM</option>\r\n\r\n  <option value=\"1\">CPAM Haute Garonne</option>\r\n\r\n  <option value=\"2\" selected>test cpam</option>\r\n\r\n  <option value=\"other\">Autre</option>\r\n</select>'''\r\n```\r\n\r\nBut of course that only works with the old django forms rendering, not the one in django-material (which django should merge upstream IMHO).\r\n\r\nAny clue welcome <3"},"comment":{"url":"https://api.github.com/repos/viewflow/django-material/issues/comments/369526218","html_url":"https://github.com/viewflow/django-material/issues/402#issuecomment-369526218","issue_url":"https://api.github.com/repos/viewflow/django-material/issues/402","id":369526218,"user":{"login":"jpic","id":94636,"avatar_url":"https://avatars3.githubusercontent.com/u/94636?v=4","gravatar_id":"","url":"https://api.github.com/users/jpic","html_url":"https://github.com/jpic","followers_url":"https://api.github.com/users/jpic/followers","following_url":"https://api.github.com/users/jpic/following{/other_user}","gists_url":"https://api.github.com/users/jpic/gists{/gist_id}","starred_url":"https://api.github.com/users/jpic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpic/subscriptions","organizations_url":"https://api.github.com/users/jpic/orgs","repos_url":"https://api.github.com/users/jpic/repos","events_url":"https://api.github.com/users/jpic/events{/privacy}","received_events_url":"https://api.github.com/users/jpic/received_events","type":"User","site_admin":false},"created_at":"2018-03-01T09:11:16Z","updated_at":"2018-03-01T09:11:16Z","author_association":"NONE","body":"Actually found a pure js solution, perhaps not perfect but works for me ;)\r\n\r\n```\r\n  // Append other option to CPAM select\r\n  $form\r\n    .find('select[name=cpam]')\r\n    .append('<option value=\"other\">Autre caisse</option>')\r\n\r\n  // http://materializecss.com/forms.html#select\r\n  $form\r\n    .find('select')\r\n    .not('.disabled')\r\n    .not('.material-ignore')\r\n    .select()\r\n\r\n```"}}
{"actor":{"display_login":"jpic"}}