我有以下AdminModel:
class ActionAdmin(admin.ModelAdmin):
list_display = (
'action_icon',
'name_',
'level',
'class_',
'category',
)
search_fields = ('name', 'category__name',)
ordering = ('level', 'name', '_class')
def name_(self, obj):
return '<strong>%s</strong>' % (obj.name)
def action_icon(self, obj):
if obj.icon:
return '<img title="%s" src="/media/%s" style="height: 42px; border-radius: 5px; -webkit-border-radius: 5px;" />' % (obj.name, obj.icon)
else:
return 'No Icon'
def class_(self, obj):
if obj == None:
return '-'
if obj._class:
return '<img title="%s" src="/media/%s" style="height: 42px; border-radius: 5px; -webkit-border-radius: 5px;" />' % (obj._class.name, obj._class.icon)
else:
return obj._class
def combo_action_(self, obj):
if obj == None:
return '-'
if obj.combo_action != None:
return '<img title="%s" src="/media/%s" style="height: 42px; border-radius: 5px; -webkit-border-radius: 5px;" />' % (obj.combo_action, obj.combo_action.icon)
else:
return obj.job
# Allow HTML for these
action_icon.allow_tags = True
name_.allow_tags = True
class_.allow_tags = True
combo_action_.allow_tags = True
# Allow ordering for these
name_.admin_order_field = 'name'
class_.admin_order_field = 'name'
现在我确实允许自定义name
和class
字段的排序,但问题是它将按照我的自定义方法(如name_(self, obj)
)返回的内容进行排序。
如何告诉AdminModel我要对该字段进行排序,特别是它的原始字段值,而不是我在方法中修改的内容?
答案 0 :(得分:2)
想出来;我将引用命名为_class
模型错误;
订购应该是:
ordering = ('level', '_class__name')
订单字段应为:
class_.admin_order_field = '_class__name'