Update custom widget

Django 3.1 updated ModelChoiceIterator behaviour
This commit is contained in:
Sonny Bakker 2021-01-17 21:08:24 +01:00
parent eb9bc2c692
commit 96dea12d1f

View file

@ -12,7 +12,14 @@ class RulesWidget(CheckboxSelectMultiple):
def create_option(self, *args, **kwargs):
option = super().create_option(*args, **kwargs)
instance = self.choices.queryset.get(pk=option["value"])
# see https://docs.djangoproject.com/en/3.1/releases/3.1/#id1
try:
pk = int(option["value"])
except TypeError:
pk = option["value"].value
instance = self.choices.queryset.get(pk=pk)
if self.category and instance.category:
option["selected"] = self.category.pk == instance.category.pk