首页 > 解决方案 > Select2 - 不显示/删除初始值

问题描述

django-filter用于过滤查询Article集。Article有一个字段locations

locations = models.ManyToManyField('locations.Location', related_name='articles')

Articles我按字段过滤locations,作为小部件,我使用django-autocomplete-light小部件Select2Multiple

class ArticleFilter(django_filters.FilterSet):
    from locations.models import Location
    ...
    locations = django_filters.ModelMultipleChoiceFilter(method='locations_filter',queryset=Location.objects.all(),label='Lokality')
    ...

    class Meta:
        model = Article
        fields = ['locations','title','price_lte','price_gte','price_currency'
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # HERE I SPECIFY THE DAL WIDGET
        self.form.fields['locations'].widget = autocomplete.Select2Multiple(url="locations:locations_autocomplete")

过滤效果很好,也可以自动完成: 在此处输入图像描述

当我单击提交时,它会正确显示过滤结果,URL 查询字符串包含所有过滤属性,包括例如locations=45,但它没有显示locationwithid=45作为locations字段的初始值。

在此处输入图像描述

奇怪的是,如果我删除小部件,我可以看到原来,有初始值:

在此处输入图像描述

只是Select2没有显示它,并且位置的值是undefinfed.

我在 raw 上使用相同的方法django.forms,它可以正常工作。首字母也是。

你知道问题出在哪里吗?

标签: javascriptjquerydjangojquery-select2django-autocomplete-light

解决方案


推荐阅读