我在django-autocomplete-light
字段上有full_name
的formset。我希望能够TAB
进入框中并输入。但是,目前,如果我选中框,我无法输入,就像ChoiceField
,但如果我点击它(或空格键),它会打开下拉菜单,我可以输入该区域< em>下面我点击的菜单,好像我输入了第一个选项。
from functools import partial, wraps
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django import forms
from extra_views import ModelFormSetView, InlineFormSetView, InlineFormSet, CreateWithInlinesView
from dal import autocomplete, forward
from .models import Record, Ledger, Person
from pdb import set_trace as st
# Create your views here.
class NameAutocomplete(autocomplete.Select2ListView):
def get_list(self):
return self.forwarded.get('full_name_choices', [])
def create_form_class(persons):
full_name_choices = [p.full_name for p in persons]
class RecordForm(forms.ModelForm):
full_name = autocomplete.Select2ListChoiceField(
widget=autocomplete.ListSelect2(
url='name_autocomplete',
forward=[
forward.Const(
full_name_choices, 'full_name_choices'
)
]
)
)
class Meta:
model = Record
fields = ['score', 'full_name', ]
return RecordForm
def create_records(request, pk):
ledger_id = pk
ledger = Ledger.objects.get(pk=ledger_id)
persons = Person.objects.filter(ledger_id=ledger_id)
RecordInlineFormSet = forms.inlineformset_factory(
Ledger,
Record,
can_delete=False,
form=create_form_class(persons),
extra=len(persons),
)
if request.method == 'POST':
formset = RecordInlineFormSet(
request.POST,
instance=ledger,
)
if formset.is_valid():
formset.save()
return HttpResponseRedirect('admin')
else:
formset = RecordInlineFormSet(
instance=ledger,
queryset=Person.objects.none(),
)
return render(
request,
'app/ledger_detail.html',
{'formset': formset},
)