你想覆蓋admin.ModelAdmin類的changelist_view方法:
def changelist_view(self, request, extra_context=None):
# just in case you are having problems with carry over from previous
# iterations of the view, always SET the self.list_display instead of adding
# to it
if something:
self.list_display = ['action_checkbox'] + ['dynamic_field_1']
else:
self.list_display = ['action_checkbox'] + ['dynamic_field_2']
return super(MyModelAdminClass, self).changelist_view(request, extra_context)
的「action_checkbox」是什麼Django使用要知道在左側的動作下降顯示覆選框下來,所以請確保在設置self.list_display時包含它。像往常一樣,如果您只是簡單地爲ModelAdmin類設置list_display,通常不需要包含它。