我有一个 Django 表单。其中一个字段 (monitoring_method) 使用自动完成灯小部件,该小部件根据另一个字段 (database_type) 中的条目过滤结果。有什么方法可以在提交之前在database_type字段中获取用户输入的值?我会知道如何使用 AJAX 来做到这一点(或者可以弄清楚),但我不确定——也许这是我真正的问题——如何将 AJAX 与自动完成结合起来。
class MonitoringMethodAutocomplete(autocomplete_light.AutocompleteBase):
autocomplete_js_attributes = {'placeholder': 'Choose a database type to enable monitoring method selection'}
def choices_for_request(self):
q = self.request.GET.get('q', '')
db_type = self.request.POST.get('database_type')
# if not db_type:
# return []
monitoring_methods = Database.objects.values_list('monitoring_method', flat=True)
return monitoring_methods.filter(database_type__exact=db_type,
name__icontains=q).distinct()
def choices_for_values(self):
return []
编辑:所以,我最初认为我试图做的事情是不可能的,但后来我意识到q
变量正在做类似的事情......那为什么不db_type
工作?