Files
pretix_cgo/src/pretix/control/forms/widgets.py
Raphael Michel e12caf186c Use Select2 for subevent and other long selections (#763)
* Use Select2 for subevent and other long selections

* Minor correction
2018-01-26 16:47:33 +01:00

39 lines
1.0 KiB
Python

from django import forms
class Select2Mixin:
template_name = 'pretixcontrol/select2_widget.html'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def options(self, name, value, attrs=None):
if value and value[0]:
for i, selected in enumerate(self.choices.queryset.filter(pk__in=value)):
yield self.create_option(
None,
self.choices.field.prepare_value(selected),
self.choices.field.label_from_instance(selected),
True,
i,
subindex=None,
attrs=attrs
)
return
def optgroups(self, name, value, attrs=None):
if value:
return [
(None, [c], i)
for i, c in enumerate(self.options(name, value, attrs))
]
return
class Select2(Select2Mixin, forms.Select):
pass
class Select2Multiple(Select2Mixin, forms.SelectMultiple):
pass