2012-08-24 55 views
26

Tôi muốn tạo bộ lọc tùy chỉnh cho quản trị django thay vì 'is_staff' và 'is_superuser' bình thường. Tôi đã đọc số này list_filter trong tài liệu Django. bộ lọc tùy chỉnh làm việc theo cách này:Tạo bộ lọc tùy chỉnh cho list_filter trong Django Admin

from datetime import date 

from django.utils.translation import ugettext_lazy as _ 
from django.contrib.admin import SimpleListFilter 

class DecadeBornListFilter(SimpleListFilter): 
    # Human-readable title which will be displayed in the 
    # right admin sidebar just above the filter options. 
    title = _('decade born') 

    # Parameter for the filter that will be used in the URL query. 
    parameter_name = 'decade' 

    def lookups(self, request, model_admin): 
     """ 
     Returns a list of tuples. The first element in each 
     tuple is the coded value for the option that will 
     appear in the URL query. The second element is the 
     human-readable name for the option that will appear 
     in the right sidebar. 
     """ 
     return (
      ('80s', _('in the eighties')), 
      ('90s', _('in the nineties')), 
     ) 

    def queryset(self, request, queryset): 
     """ 
     Returns the filtered queryset based on the value 
     provided in the query string and retrievable via 
     `self.value()`. 
     """ 
     # Compare the requested value (either '80s' or '90s') 
     # to decide how to filter the queryset. 
     if self.value() == '80s': 
      return queryset.filter(birthday__gte=date(1980, 1, 1), 
            birthday__lte=date(1989, 12, 31)) 
     if self.value() == '90s': 
      return queryset.filter(birthday__gte=date(1990, 1, 1), 
            birthday__lte=date(1999, 12, 31)) 

class PersonAdmin(ModelAdmin): 
    list_filter = (DecadeBornListFilter,) 

Nhưng tôi đã thực hiện các chức năng tùy chỉnh cho list_display như thế này:

def Student_Country(self, obj): 
    return '%s' % obj.country 
Student_Country.short_description = 'Student-Country' 

Có thể tôi có thể sử dụng các chức năng tùy chỉnh cho list_display trong list_filter thay vì viết một hàm tùy chỉnh mới cho list_filter? Bất kỳ đề xuất hoặc cải tiến nào đều được hoan nghênh .. Cần một số hướng dẫn về điều này ... Cảm ơn ...

+0

http: // stackoverflow. com/questions/12215751/can-i-make-list-filter-in-django-admin-chỉ-hiển thị-tham chiếu-ngoại ngữ – user956424

Trả lời

1

list_display, phương thức trả về một chuỗi, nhưng nếu tôi hiểu chính xác, điều bạn muốn làm là thêm bộ lọc cho phép lựa chọn của các nước sinh viên, đúng không?

Đối với bộ lọc quan hệ đơn giản này, và trên thực tế đối với cột hiển thị danh sách "Sinh viên-Quốc gia", bạn không cần tạo lớp bộ lọc tùy chỉnh cũng như phương thức hiển thị danh sách tùy chỉnh; điều này sẽ đủ:

class MyAdmin(admin.ModelAdmin): 
    list_display = ('country',) 
    list_filter = ('country',) 

Cách django không list_filter, như được giải thích in the docs, là lần đầu tiên bằng cách tự động phù hợp với lĩnh vực bạn cung cấp cho xây dựng trước lớp lọc; các bộ lọc này bao gồm CharField và ForeignKey.

list_display tương tự tự động hóa dân số của cột danh sách thay đổi bằng cách sử dụng lĩnh vực thông qua bằng cách lấy các đối tượng có liên quan và trả lại unicode giá trị trong số này (tương tự như trong phương pháp mà bạn cung cấp ở trên).

5

Bạn thực sự có thể thêm bộ lọc tùy chỉnh vào bộ lọc quản trị bằng cách mở rộng SimpleListFilter. Ví dụ, nếu bạn muốn thêm một bộ lọc lục cho 'Phi' để lọc nước quản trị sử dụng ở trên, bạn có thể làm như sau:

Trong admin.py:

from django.contrib.admin import SimpleListFilter 

class CountryFilter(SimpleListFilter): 
    title = 'country' # or use _('country') for translated title 
    parameter_name = 'country' 

    def lookups(self, request, model_admin): 
     countries = set([c.country for c in model_admin.model.objects.all()]) 
     return [(c.id, c.name) for c in countries] + [ 
      ('AFRICA', 'AFRICA - ALL')] 

    def queryset(self, request, queryset): 
     if self.value() == 'AFRICA': 
      return queryset.filter(country__continent='Africa') 
     if self.value(): 
      return queryset.filter(country__id__exact=self.value()) 

class CityAdmin(ModelAdmin): 
    list_filter = (CountryFilter,) 
Các vấn đề liên quan