class Media in Django formsets

35 Views Asked by At

I have a form named SpecificDateForm. Additionally, I have a formset named SpecificDateFormset, which is created using Django's inlineformset_factory. This formset uses HiddenDeleteInputFormset, a custom formset that, as the name suggests, hides the delete checkbox.

from django import forms

class SpecificDateForm(forms.ModelForm):
    class Meta:
        model = SpecificDate
        fields = ['date',]
        labels = {
            'date': 'Datum',
        }
        widgets = {
            'date': DatePickerInput(),
       }
        help_texts = {
            'date': 'fix vordefinierter Termin',
        }


class HiddenDeleteInputFormSet(forms.BaseInlineFormSet):
    def add_fields(self, form, index):
        super().add_fields(form, index)
        if 'DELETE' in form.fields:
            form.fields['DELETE'].widget = forms.HiddenInput()


SpecificDateFormset = forms.inlineformset_factory(
    Directive, SpecificDate,
    form=SpecificDateForm,
    formset=HiddenDeleteInputFormSet,
    extra=1,
    can_delete=True,
    localized_fields=['date']
)

I recently learned that with class Media I can inject a custom javascript that loads with the form. Does this work with formsets? I can't get it to work. I tried class Media at various locations in the code, but my custom javascript never shows up in the {{ my_formset.media }} tag.

So my question is: Where do I define class Media? Or is it simply impossible with formsets?

UPDATE:

After testing further I think my problem lies in the usage of the DatePickerInput() widget out of django-bootstrap-datepicker-plus. It seems the widget overwrites whatever I'm defining in class Media within my forms.

I started tinkering with the widget but find it most uncooperative.

DatePickerInput subclasses BasePickerInput which in turn is based on django.forms.widgets.DateTimeBaseInput.

I now try to extend this by doing:

from django.forms.widgets import Media as FormsMedia
from bootstrap_datepicker_plus.widgets import BasePickerInput

class SpecificDateForm(forms.ModelForm):
    class Media:
        js = BasePickerInput.media.__get__('js', []) + FormsMedia(
            js=["dienstabfolge/js/dynamic_dates.js"])

    ...

The above works in the sense that my custom javascript is now part of the media.js list, BUT when I reload the page itself I run into the error

  File ".../django/forms/widgets.py", line 95, in render
    "\n".join(
  File ".../django/forms/widgets.py", line 97, in <genexpr>
    getattr(self, "render_" + name)() for name in MEDIA_TYPES
  File ".../django/forms/widgets.py", line 107, in render_js
    for path in self._js
  File ".../django/forms/widgets.py", line 91, in _js
    return self.merge(*self._js_lists)
  File ".../django/forms/widgets.py", line 157, in merge
    head = list_[0]
  File ".../django/forms/widgets.py", line 142, in __getitem__
    raise KeyError('Unknown media type "%s"' % name)
KeyError: 'Unknown media type "0"'
0

There are 0 best solutions below