Django admin - 在选择字段上使用自动完成和过滤器(不是 ManyToMany 或 ForeignKey)

Django admin - using autocomplete and filter on choices fields (not ManyToMany or ForeignKey)

这是我的模型:

class Academic(models.Model):

SCHOOL_COLLEGE_SERVICE = [
    ('School Of Humanities', 'School Of Humanities'),
    ('School Of Culture & Creative Arts', 'School Of Culture & Creative Arts'),
    ('School Of Modern Languages & Cultures', 'School Of Modern Languages & Cultures'),
    ('School Of Critical Studies', 'School Of Critical Studies'),
    ('Arts  College Of Arts Administration', 'Arts  College Of Arts Administration'),
    ]

school = models.CharField(choices=SCHOOL_COLLEGE_SERVICE, max_length=50, blank=True, null=True)

我希望在我的 Django 管理界面中有一个很好的自动完成/过滤器。不幸的是,如果数据集不是来自 ManyToMany 或 ForeignKey 关系,则似乎无法自动完成。这是我试过的:

from django.contrib import admin
from .models import Academic, Partner, Project
from admin_auto_filters.filters import AutocompleteFilter
import django_filters

@admin.register(Academic)
class AcademicAdmin(admin.ModelAdmin):
    search_fields = ['surname', 'forename']
    #school = django_filters.ChoiceFilter(choices=Academic.SCHOOL_COLLEGE_SERVICE)
    #autocomplete_fields = ['school']

我知道我也可以像这样设置一个查询集:

class SchoolFilter(django_filters.FilterSet):
    class Meta:
        model = Academic
        fields = ['school',]

但 Django 仍然抱怨 The value of 'autocomplete_fields[0]' must be a foreign key or a many-to-many field. 我怎样才能达到我想要的?

您可以使用 django-autocomplete-light 包轻松地将表单 choicefield 作为自动完成下拉列表。这个包还支持查询集。

在forms.py,

class AcademicForm(forms.ModelForm):
    SCHOOL_COLLEGE_SERVICE = [
    ('School Of Humanities', 'School Of Humanities'),
    ('School Of Culture & Creative Arts', 'School Of Culture & Creative Arts'),
    ('School Of Modern Languages & Cultures', 'School Of Modern Languages & Cultures'),
    ('School Of Critical Studies', 'School Of Critical Studies'),
    ('Arts  College Of Arts Administration', 'Arts  College Of Arts Administration'),
    ]
    school = forms.ChoiceField(choices=SCHOOL_COLLEGE_SERVICE,
                               widget=autocomplete.ListSelect2(url='school_autocomplete'))
    
    class Meta:
        model = Academic
        fields = ['school', 'other_your_model_fields']

在urls.py,

urlpatterns = [
    path('school-autocomplete', SchoolAutocompleteView.as_view(), name='school_autocomplete'),
    ]

在views.py,

from dal import autocomplete

class SchoolAutocompleteView(autocomplete.Select2ListView):

    def get_list(self):
        if not self.request.user.is_authenticated:
            return []

        SCHOOL_COLLEGE_SERVICE = ['School Of Humanities',
                                  'School Of Culture & Creative Arts',
                                  'School Of Modern Languages & Cultures',
                                  'School Of Critical Studies',
                                  'Arts  College Of Arts Administration']

        return SCHOOL_COLLEGE_SERVICE

现在你在你的管理中使用 modelform class,

@admin.register(Academic)
class AcademicAdmin(admin.ModelAdmin):
    form = AcademicForm
    ....
    ....

更多详情,doc_link-https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html