I was expecting to see a single functioning widget, but instead have two partially functioning ones.
I have a Work and a Parent Work; each Work can have 0 or 1 ParentWork.
models.py
class ParentWork(UUIDModel):
url_name = "parent_work_detail"
class Work(models.Model):
parent_work = models.ForeignKey(ParentWork, models.SET_NULL, null=True, blank=True)
... other fields
I am using DAL for forms: forms.py
class ParentWorkForm(forms.ModelForm):
related_works = forms.ModelMultipleChoiceField(
label="Related Works",
queryset=Work.objects.all().order_by("title"),
required=False,
widget=autocomplete.ModelSelect2Multiple(url="autocomplete_work"),
)
class Meta:
model = ParentWork
exclude = []
required = ["name"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# for the Parent Works update form, set the initial value of the related_works field
try:
instance = kwargs.get("instance")
if instance:
self.fields["related_works"].initial = instance.work_set.all()
except Exception as e:
print(e)
I have to declare related_works on the ModelForm as it is stored on the Work, not ParentWork.
Relevant snippet from parent_work_form.html template:
Crossposting from StackOverflow, didn't get any traction there and coming back to this problem. https://stackoverflow.com/questions/77001904/django-autocomplete-light-select2-generates-multiple-selectboxes-for-declarative
I was expecting to see a single functioning widget, but instead have two partially functioning ones.
I have a Work and a Parent Work; each Work can have 0 or 1 ParentWork.
models.py
I am using DAL for forms:
forms.py
I have to declare related_works on the ModelForm as it is stored on the Work, not ParentWork.
Relevant snippet from
parent_work_form.html
template:Other forms inheriting from Meta.widgets on ModelForms are fine, but this declared field is generating two widgets rather than one.
Two widgets for "Related Works":![Two widgets for Related Works](https://i.stack.imgur.com/yZrsx.png)
The top box seems to function correctly in terms of search:![top box with functioning autocomplete](https://i.stack.imgur.com/tcl78.png)
But the bottom box only shows options that have already been selected in the top box.
![bottom box - only options from top box selections](https://i.stack.imgur.com/S3MIt.png)
On the flip side, if I submit my form with selected options in the top box but not the bottom, nothing is saved.![Rendered HTML screenshot](https://i.stack.imgur.com/oU2vA.png)