简体   繁体   English

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

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

This is my model:这是我的模型:

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)

I'd like to have a nice autocomplete / filter in my Django administration interface.我想在我的 Django 管理界面中有一个很好的自动完成/过滤器。 Unfortunately it seems that it is not possible to have autocomplete if the dataset doesn't come from a ManyToMany or ForeignKey relationship.不幸的是,如果数据集不是来自多对多或外键关系,似乎不可能有自动完成功能。 This is what I tried:这是我尝试过的:

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']

I know I can also set a queryset like so:我知道我也可以像这样设置查询集:

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

But Django still complains that The value of 'autocomplete_fields[0]' must be a foreign key or a many-to-many field.但是 Django 仍然抱怨The value of 'autocomplete_fields[0]' must be a foreign key or a many-to-many field. How can I achieve what I want?我怎样才能达到我想要的?

You can make a form choicefield as a autocomplete dropdown easily using django-autocomplete-light package.您可以使用django-autocomplete-light包轻松地将表单选择字段作为自动完成下拉列表。 This package also supports queryset.这个包也支持查询集。

In forms.py,在forms.py中,

class AcademicForm(forms.ModelForm):
    school = forms.ChoiceField(choices=[],
                               widget=autocomplete.ListSelect2(url='school_autocomplete'))
    
    class Meta:
        model = Academic
        fields = ['school', 'other_your_model_fields']

In urls.py,在 urls.py 中,

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

In views.py,在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

Now you use modelform in your admin class,现在您在管理类中使用 modelform,

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

for more details, doc_link- https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html有关更多详细信息,请参阅 doc_link- https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用filter_horizo​​ntal在django admin中显示多对多字段的所有可用选择 - How to display all available choices of a manytomany field in django admin using filter_horizontal 根据相同模型多对多字段中的选择过滤管理表单中的外键选择? - Filter ForeignKey choices in an Admin Form based on selections in same models ManytoMany Field? Django 包含在 ManyToMany 中的 ForeignKey 过滤器 - Django filter for ForeignKey included in ManyToMany Django ForeignKey.limit_choices_to with ForeignKey to ManyToMany 场景 - Django ForeignKey.limit_choices_to with ForeignKey to ManyToMany scenario 在django admin中通过ForeignKey过滤 - filter by ForeignKey in django admin 如何过滤 Django2 的 autocomplete_fields 中的选择? - How to filter choices in Django2's autocomplete_fields? Django - 根据对象的外键过滤对象但有多种选择? - Django - Filter objects based on their ForeignKey but with multiple choices? Django-如何在管理页面上的表格的可编辑列中过滤ForeignKey选择? - Django - how do I filter ForeignKey choices in an editable column in a table on the admin page? 在Django管理面板中过滤ForeignKey - Filter ForeignKey in django admin panel 在 Django Model 中使用水平过滤器修改多对多字段的可用和选择选项 - Modifying Available and Chosen Choices For ManyToMany Field With Horizontal Filter In Django Model Admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM