Django formset 不适用于双选择器小部件

问题描述 投票:0回答:0

我学会了使用 django formset,尤其是双选择器小部件:

#forms.py
class TestExamForm(forms.ModelForm):
    topics = models.ModelMultipleChoiceField(
        queryset=Topic.objects.all(),
        widget=DualSelector(search_lookup='name__icontains', group_field_name='subject',),
    )

    class Meta:
        model = Exam4Fin
        fields = '__all__'

#views.py
def detail(request):
    context = {}
    # create a formset for Books
    BookFormSet = modelformset_factory(Exam4Fin, form=TestExamForm, extra=1)

    if request.method == 'POST':
        # validate formset and save data
        formset = BookFormSet(request.POST, queryset=Topic.objects.none())
        if formset.is_valid():
            instances = formset.save(commit=False)
            for instance in instances:
                instance.save()
            return #redirect('book_author')
    else:
        # display empty formset
        formset = BookFormSet(queryset=Topic.objects.none())

    context = {
        'formset': formset,
       # 'authors': authors
    }
    #return render(request, 'book_author.html', context)
    return render(request, "testexam.html", context)

#html template

<form method="POST">
    {% csrf_token %}
    {{ formset.management_form }}
    {% for form in formset %}
        {{ form.as_p }}
    {% endfor %}
    <button type="submit">Save</button>

</form>

一切正常......但 html 页面看起来很奇怪:

布局看起来很奇怪。在检查元素时出现这样的错误:

Uncaught Error: Attempt to initialize <django-selectize> outside <django-formset>
    at new d (chunk-EVLKIONU.js:1:278)
    at new h (DualSelector-NEQ42NHO.js:1:294)
    at HTMLSelectElement.connectedCallback (DualSelector-NEQ42NHO.js:1:12262)
    at django-formset.js:25:31235

有人能帮我吗我一无所知....

django formset
© www.soinside.com 2019 - 2024. All rights reserved.