I have a formset with django-autocomplete-light
on the full_name
field. I want to be able to TAB
into the box and type. Currently, however, if I tab into the box, I can't type, it's like a ChoiceField
, but if I click on it (or spacebar) it opens up the dropdown menu and I can type into the area below the menu I clicked on, as though I was typing in the first choice option.
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},
)