How to give a column in the Django admin change_list a CSS class

后端 未结 3 2000
梦如初夏
梦如初夏 2021-01-06 09:44

I\'d like to change the column widths in the list display of the Django admin.

Is it possible somehow to add a CSS classname to a column? I\'d preferably not overwri

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 10:35

    While this feature is implemented in vers1.6 as StvnW said, for earlier versions you can do the following:

    In admin.py:

    class MyModelAdmin(admin.ModelAdmin):
        # your code here
    
        # specify a stylesheet just for the list view
        class Media:
            css = {'all': ('css/mymodel_list.css')}
    

    In mymodel_list.css:

    /* replace '5' with the column desired */
    table#result_list td:nth-child(5) {
        width: 15em;
    }
    

    Specifying table#result_list will apply this stylesheet only to the list view and won't affect the normal admin page for this model. Also, note that while django uses th for the first column of the model, it still counts for a td child.

提交回复
热议问题