我正在尝试使用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)
我在第一次点击时在模态引导下应用它来显示包含表单的模态,它不会在第二次单击后显示带有选项的选择。
提前致谢
答案 0 :(得分:0)
我解决了它只是在View.py中删除:
using Google.Apis.AnalyticsReporting.v4;
using Google.Apis.Services;
using System;
using Google.Apis.AnalyticsReporting.v4.Data;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using Google.Apis.Auth.OAuth2;
using System.Threading;
using Google.Apis.Util.Store;
using System.Web;
public static class GoogleAnalyticsAPI
{
static string vmcApiKey = "<<API-KEY>>";
public static void Test()
{
AnalyticsReportingService ars = GetService(vmcApiKey);
// Create the DateRange object.
DateRange dateRange = new DateRange() { StartDate = "2017-01-01", EndDate = "2017-04-28" };
// Create the Metrics object.
Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };
//Create the Dimensions object.
Dimension browser = new Dimension { Name = "ga:browser" };
// Create the ReportRequest object.
// Create the ReportRequest object.
ReportRequest reportRequest = new ReportRequest
{
ViewId = "<<VIEW-ID>>",
DateRanges = new List<DateRange>() { dateRange },
Dimensions = new List<Dimension>() { browser },
Metrics = new List<Metric>() { sessions }
};
List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);
// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };
// Call the batchGet method.
GetReportsResponse response = ars.Reports.BatchGet(getReport).Execute();
}
public static AnalyticsReportingService GetService(string apiKey)
{
try
{
if(string.IsNullOrEmpty(apiKey))
throw new ArgumentNullException("api Key");
return new AnalyticsReportingService(new BaseClientService.Initializer()
{
ApiKey = apiKey,
ApplicationName = "AnalyticsReporting API key example",
});
}
catch(Exception ex)
{
throw new Exception("Failed to create new AnalyticsReporting Service", ex);
}
}
因为函数mixup_querysets将限制每个查询集。