Skip to content

Instantly share code, notes, and snippets.

@czpython
Created November 27, 2013 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czpython/7683425 to your computer and use it in GitHub Desktop.
Save czpython/7683425 to your computer and use it in GitHub Desktop.
Django Readonly Select2 widget https://github.com/applegrew/django-select2
# -*- coding: utf-8 -*-
from django_select2.widgets import Select2MultipleWidget
class ReadonlySelect2MultipleWidget(Select2MultipleWidget):
def render_inner_js_code(self, id_, *args):
"""
Renders all the JS code required for this widget.
:return: The rendered JS code which will be later enclosed inside ``<script>`` block.
:rtype: :py:obj:`unicode`
"""
options = dict(self.get_options())
options = self.render_select2_options_code(options, id_)
# I know how this looks, but setting the "readonly" option to true is not enough :(
js = u"""
$("#{id}").select2({options});
$("#{id}").select2("readonly", true);
"""
return js.format(id=id_, options=options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment