我有一个字段“ car”,其中包含值“ Benz,Nissan,Kia” 我使用==> list_filter [“ car”]如何按“日产”和“起亚”这两个值进行过滤。两者都不是
答案 0 :(得分:0)
您可以使用django-admin-multiple-choice-list-filter软件包中的MultipleChoiceListFilter
,因为Django中没有内置多选过滤器。示例:
models.py
class CarModel(models.Model):
name = models.CharField(max_length=255)
class Car(models.Model):
car_model = models.ForeignKey(CarModel, on_delete=models.CASCADE)
admin.py
from django.contrib import admin
from django_admin_multiple_choice_list_filter.list_filters import MultipleChoiceListFilter
from .models import Car
class CarModelListFilter(MultipleChoiceListFilter):
title = 'Car Model'
parameter_name = 'car_model__in'
def lookups(self, request, model_admin):
return CarModel.objects.values_list('pk', 'name')
class CarAdmin(admin.ModelAdmin):
list_display = ('car_model',)
list_filter = (CarModelListFilter,)
admin.site.register(Car, CarAdmin)