2010-09-05 64 views

回答

2

設置fields一個的hackish辦法做到這一點是覆蓋list_display變量每次修改列表視圖稱爲:

class MyModelAdmin(admin.ModelAdmin) 
    def changelist_view(self, request, extra_context=None): 
     user = request.user 
     if user.is_superuser: 
      self.list_display = ['field1', 'field2'] 
     else: 
      self.list_display = ['field1'] 
     return super(MyModelAdmin, self).changelist_view(request, extra_context=None) 

但設置每次都變爲期望的值,因爲同樣的管理實例也可能被其他用戶的請求調用!

+0

做出對不起我的問題是錯誤的,我真正的意思'fields'而不是'list_display',但你的建議仍然適用 – zer0stimulus 2010-09-05 19:07:57

1

改變編輯窗體的一個有趣的方法是隻改變只讀字段列表。

爲此只覆蓋功能get_readonly_fields

class MyModelAdmin(admin.ModelAdmin): 
    def get_readonly_fields(self, request, obj=None): 
     user = request.user 
     if user.is_superuser: 
      readonly = [] 
     else: 
      readonly = ['field1'] 
     return readonly 

關於list_display現代的方法來修改它是重新定義get_list_display

原來的定義只是返回self.list_display

class ModelAdmin: 
    def get_list_display(self, request): 
     """ 
     Return a sequence containing the fields to be displayed on the 
     changelist. 
     """ 
     return self.list_display 

,這樣可以提供:

class MyModelAdmin(admin.ModelAdmin): 
    def get_list_display(self, request): 
     user = request.user 
     if user.is_superuser: 
      list_display = ['field1', 'field2'] 
     else: 
      list_display = ['field1'] 
     return list_display 
相關問題