Skip to content

Instantly share code, notes, and snippets.

@czpython

czpython/widgets.py

Created Nov 27, 2013
Embed
What would you like to do?
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
You can’t perform that action at this time.