0

是否有限制ModelView列的大小(長度/寬度)的方法?我使用所見即所得的編輯器,這會創建非常長的文本,從而使模型視圖的列非常長。減少Flask-Admin中列的大小

下面是它的樣子。看右邊最後一列。它甚至比截圖可以處理的時間還要長。

enter image description here

回答

3

不要(排除法)顯示列:

class MyView(ModelView): 

    column_exclude_list = ('description') 

不顯示的列(由包含):

class MyView(ModelView): 

    column_list = ('rating', 'category_id', 'year', 'stock', 'image') 

格式化列:

class MyView(ModelView): 

    def _description_formatter(view, context, model, name): 
     # Format your string here e.g show first 20 characters 
     # can return any valid HTML e.g. a link to another view to show the detail or a popup window 
     return model.description[:20] 

    column_formatters = { 
     'description': _description_formatter, 
    } 
+0

謝謝!重新格式化爲我做了! – kstullich

+0

在切片之前不需要檢查邊界,因爲切片索引是由Python處理的。第6行可以縮短爲'return model.description [:20]' – MrLeeh

+0

@MrLeeh - 謝謝 - 現在改正了。 – pjcunningham

0

辦法做到這可能是覆蓋相關列的CSS樣式。在燒瓶管理員list.html模板,你會發現下面的代碼創建的列:

{% for c, name in list_columns %} 
<td class="col-{{c}}"> 
    {% if admin_view.is_editable(c) %} 
    {% set form = list_forms[get_pk_value(row)] %} 
    {% if form.csrf_token %} 
     {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c), csrf=form.csrf_token._value()) }} 
    {% else %} 
     {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c)) }} 
    {% endif %} 
    {% else %} 
    {{ get_value(row, c) }} 
    {% endif %} 
</td> 
{% endfor %} 

所以如對於第2列,您可以向css類col-2添加max-width屬性以限制其寬度。