django-autocomplete-light with gm2m in modals bootstrap: select 未显示在模板中

django-autocomplete-light with gm2m in modals bootstrap: the select not showing in template

我正在尝试使用 Gm2m 获得自动完成功能,但 autocomplete.GM2MQuerySetSequenceField 并未显示所有选项。

View.py

class RessourceAutocompleteView(Select2QuerySetSequenceView):

def get_queryset(self):
        employees = Employees.objects.all()
        freecontracts = Frecontract.objects.filter(IS_ACTIVE=True)
        freeagencies = Freagencies.objects.filter(IS_ACTIVE=True)
        freelancers = Freelancers.objects.filter(IS_ACTIVE=True)

        if self.q:
            employees = Employees.objects.filter(EMP_FIRST_NAME__icontains=self.q)
            freecontracts = Frecontract.objects.filter(FIRST_NAME__icontains=self.q,
                                                       IS_ACTIVE=True)
            freeagencies = Freagencies.objects.filter(AG_NAME__icontains=self.q,
                                                      IS_ACTIVE=True)
            freelancers = Freelancers.objects.filter(FRE_FIRST_NAME__icontains=self.q,
                                                     IS_ACTIVE=True)
            #

        # Aggregate querysets
        qs = QuerySetSequence(employees, freecontracts, freeagencies, freelancers)
        # qs = QuerySetSequence(employees)

        if self.q:
            # This would apply the filter on all the querysets
            qs = qs.filter(Q(EMP_FIRST_NAME__icontains=self.q)|
                           Q(FIRST_NAME__icontains=self.q)|
                           Q(AG_NAME__icontains=self.q) |
                           Q(FRE_FIRST_NAME__icontains=self.q
                          ))

        # This will limit each queryset so that they show an equal number
        # of results.
        qs = self.mixup_querysets(qs)

        return qs

Forms.py :

class QAMForm(autocomplete.FutureModelForm):
def __init__(self, *args, **kwargs):

    super(QAMForm, self).__init__(*args, **kwargs)
    for field in iter(self.fields):
        self.fields[field].widget.attrs.update({
            'class': 'form-control'
        })

TRANSLATOR = autocomplete.GM2MQuerySetSequenceField(
    queryset=autocomplete.QuerySetSequence(
        Employees.objects.all(),
        Frecontract.objects.all(),
        Freagencies.objects.all(),
        Freelancers.objects.all(),
    ),
    required=False,
    label=_("Traducteur(s)"),
    widget=autocomplete.QuerySetSequenceSelect2Multiple(
        'qm:ressource-autocomplete'),
)

PROOFREADER = autocomplete.GM2MQuerySetSequenceField(
    queryset=autocomplete.QuerySetSequence(
     # all models 
        Employees.objects.all(),
        Frecontract.objects.all(),
        Freagencies.objects.all(),
        Freelancers.objects.all(),
    ),
    required=False,
    label=_("Relecteur(s)"),
    widget=autocomplete.QuerySetSequenceSelect2Multiple(
        'qm:ressource-autocomplete'),
)
PM = forms.ModelMultipleChoiceField(queryset=Employees.objects.filter(TYPE_RESOURCE__TYPE__in = ('PMJ', 'PMS')),
                                    required=False, label=_("PM(s)"),)
# QA = forms.ModelChoiceField(queryset=Employees.objects.filter(TYPE_RESOURCE__TYPE__in=('PMJ', 'PMS')),
#                            required=False)

class Meta:
    model = Feedback
    fields = ['PM']

Models.py:

class Feedback(models.Model):
#the fields
TRANSLATOR = GM2MField()
PROOFREADER = GM2MField()
PM = models.CharField(max_length=250,  blank=True, null=True)

我在第一次点击时在模态 bootstrap 下应用它以显示包含表单的模态,它不显示 select 以及第二次点击后的选项。

提前致谢

我解决了它只是在 View.py 中消除:

qs = self.mixup_querysets(qs)

因为函数mixup_querysets会限制每个查询集。