gpt4 book ai didi

django - 在管理中列出过滤器和搜索结果

转载 作者:行者123 更新时间:2023-12-02 17:40:13 26 4
gpt4 key购买 nike

在我重写 get_search_results 方法后,list_filter 不起作用,但搜索字段按我的预期工作。

class NaruceniProduktiAdmin(admin.ModelAdmin):
search_fields = ('broj_narudbe', )
list_filter = ('date', 'status', )
list_display = (
'naziv_produkta', 'datum', 'narudba_broj', 'boja', 'velicina', 'cijena', 'kolicina',
'ukupna_cijena','korisnik_link','status','source_link',
)
actions = [dostupan, nedostupan, email_dostupan, placen, posalji_racun, poslan, isporucen, storniran, posalji_storno, ]

def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super(NaruceniProduktiAdmin, self).get_search_results(request, queryset, search_term)

try:
search_term_as_int = int(search_term)
except ValueError:
search_term_as_int=search_term.strip()
queryset |= self.model.objects.filter(korisnik__ime__icontains=search_term_as_int)

if not queryset:
queryset |= self.model.objects.filter(korisnik__prezime__icontains=search_term_as_int)
else:
queryset = self.model.objects.filter(broj_narudbe=search_term_as_int)

return queryset, use_distinct

如果我删除 get_search_results,则 list_filter 按预期工作。我想保留它,因为我希望搜索字段按照我的预期工作,但我也希望列表过滤像平常一样工作。

有没有办法可以同时使用这两个功能?一个人影响另一个人是否有原因?

enter image description here

最佳答案

Changelist View 首先进行过滤,然后将过滤后的查询集作为参数提供给您的 get_search_results 函数。为了使其正常工作,您应该使用 queryset 参数而不是 self.model.objects

def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super(NaruceniProduktiAdmin, self).get_search_results(request, queryset, search_term)

try:
search_term_as_int = int(search_term)
except ValueError:
search_term_as_int=search_term.strip()
new_queryset |= queryset.filter(korisnik__ime__icontains=search_term_as_int)

if not new_queryset:
new_queryset |= queryset.filter(korisnik__prezime__icontains=search_term_as_int)
else:
new_queryset = queryset.filter(broj_narudbe=search_term_as_int)

return new_queryset, use_distinct

关于django - 在管理中列出过滤器和搜索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27573742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com