立即在框中输入文字以进行自动填充

时间:2017-09-05 03:45:28

标签: python django forms django-autocomplete-light

我在django-autocomplete-light字段上有full_name的formset。我希望能够TAB进入框中并输入。但是,目前,如果我选中框,我无法输入,就像ChoiceField,但如果我点击它(或空格键),它会打开下拉菜单,我可以输入该区域< em>下面我点击的菜单,好像我输入了第一个选项。

enter image description here

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},
    )

0 个答案:

没有答案