Django s2forms.ModelSelect2Widget not works properly

hi all I’m trying using ModelSelect2Widget I set redis server which I test and it works. then I set the following project:

models.py

class Doctor(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    status=models.BooleanField(default=True)
    def __str__(self):
        return "{} ({})".format(self.user.first_name,self.department)

class Patient(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)
    assignedDoctorId = models.ForeignKey(Doctor, on_delete=models.CASCADE,related_name='doctor_assigned')
    admitDate=models.DateField(auto_now=True)
    status=models.BooleanField(default=False)
    def __str__(self):
        return self.user.first_name

form.py

class BaseAutocompleteSelect(s2forms.ModelSelect2Widget):
    class Media:
        js = ("admin/js/vendor/jquery/jquery.min.js",)

    def __init__(self, **kwargs):
        super().__init__(kwargs)
        self.attrs = {"style": "width: 300px"}

    def build_attrs(self, base_attrs, extra_attrs=None):
        base_attrs = super().build_attrs(base_attrs, extra_attrs)
        base_attrs.update(
            {"data-minimum-input-length": 10, "data-placeholder": self.empty_label}
        )
        return base_attrs
    
class DoctorAutocompleteWidget(BaseAutocompleteSelect):
    empty_label = "-- select doctor --"
    search_fields = ("username__icontains",)
    queryset=models.Doctor.objects.all().filter(status=True).order_by("id")    

class PatientForm(forms.ModelForm):
    assignedDoctorId=forms.ModelChoiceField(queryset=models.Doctor.objects.all().filter(status=True),
                                            widget=DoctorAutocompleteWidget)

but results is an empty list enter image description here while using

assignedDoctorId=forms.ModelChoiceField(queryset=models.Doctor.objects.all().filter(status=True),empty_label="Name and Department")

it show me list but I would like use select2 in order to user redis and the search bar

I would like create select and multiselect menu with searchbar to change list value: in the future I would like the same with table list and change a dropdown menu option if user insert a string in an input module or select an option from one other dropdown menu

Вернуться на верх