1 from django import forms
2 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
4 from emissions.models import SoundFile
6 from .models import SoundCell
7 from .views import soundfiles
9 class SoundFileWidget(HeavySelect2Widget):
10 def render_texts(self, selected_choices, choices):
11 queryset = SoundFile.objects.filter(id__in=selected_choices)
13 return '%s - %s - %s' % (soundfile.episode.emission.title,
14 soundfile.episode.title,
15 soundfile.title or soundfile.id)
16 texts = [fmt(soundfile) for soundfile in queryset.select_related()]
17 return convert_to_js_string_arr(texts)
20 class SoundCellForm(forms.ModelForm):
23 fields = ('soundfile',)
25 def __init__(self, *args, **kwargs):
26 super(SoundCellForm, self).__init__(*args, **kwargs)
27 self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)