Django admin上list_display项的自定义值

时间:2015-10-08 11:00:16

标签: python django django-admin

在模型中,我有一个字幕字段,如果给定对象的字段不存在值,则填充None

是否可以将值显示为自定义内容(例如Not AvailableNot Applicable,而不仅仅显示(None)

字段

sub_title = models.CharField(max_length=255, null=True, blank=True) 

管理员

 list_display = 'sub_title', 

enter image description here

PS:我想在数据库中使用None,而在管理面板上只需要一个自定义值。

感谢

2 个答案:

答案 0 :(得分:9)

SCOM_ConnectionSettings(); Guid alertId = Guid.Empty; //StreamReader streamReader = new StreamReader(alertIDsToClose); //streamReader. Dictionary<string, string> alertIDs = JsonConvert.DeserializeObject<Dictionary<string, string>>(alertIDsToClose); #region test //String str = WebOperationContext.Current.IncomingRequest.Accept.ToString(); //StreamReader reader = new StreamReader(alertIDs); //String res = reader.ReadToEnd(); //NameValueCollection coll = HttpUtility.ParseQueryString(res); //foreach(string alertID in alertIDs) //{ #endregion foreach (KeyValuePair<string, string> alertID in alertIDs) { alertId = new Guid(alertID.Value); } MonitoringAlert monitoringAlert = mgGroup.GetMonitoringAlert(alertId); ReadOnlyCollection<MonitoringAlertResolutionState> alertStates = mgGroup.GetMonitoringAlertResolutionStates(); MonitoringAlertResolutionState closedState = null; foreach (MonitoringAlertResolutionState thisState in alertStates) { if (thisState.Name == "Closed") { closedState = thisState; } } if (monitoringAlert.ResolutionState != closedState.ResolutionState) { monitoringAlert.ResolutionState = closedState.ResolutionState; string comment = "closing availability alert"; monitoringAlert.Update(comment); } } 可以接受可调用,因此您可以执行此操作:

list_display

docs提供了其他几个用于提供可调用的选项。

答案 1 :(得分:8)

如果您使用 empty_value_display

会更好
  

empty_value_display
  此属性会覆盖记录的空字段(无,空字符串等)的默认显示值。默认值为 - (破折号)。

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('sub_title',)
    sub_title.empty_value_display = 'Not Available'

这适用于django >= 1.9