是否可以在django-admin的某個模型的列表顯示頁面上放置模型描述或描述?在django-admin中的模型描述
我在說的是,當你點擊django-admin主頁上的模型名稱鏈接,以及當你轉到該模型的列表顯示頁面時。表格上方會有描述。像
東西「這種模式是用於記錄將通過我們的刮中獲取所有帳戶....等等」
類似的東西,這可能嗎?
是否可以在django-admin的某個模型的列表顯示頁面上放置模型描述或描述?在django-admin中的模型描述
我在說的是,當你點擊django-admin主頁上的模型名稱鏈接,以及當你轉到該模型的列表顯示頁面時。表格上方會有描述。像
東西「這種模式是用於記錄將通過我們的刮中獲取所有帳戶....等等」
類似的東西,這可能嗎?
這將是一個非常好的功能被添加到Django的管理員核心。在此之前,您可以快速瀏覽您的問題。
讓我們假設你要打印的每個模型的docstring
,就像這樣:
class MyModel(models.Model):
"""
I wanna get printed in the Admin!
"""
# model fields here
所以,你想打印在change_list
頁。好。
創建custom template tag(或者您的應用程序中或創建另一個應用程序,將持有全局模板標籤/過濾)是這樣的:
from django import template
from django.utils.html import mark_safe
register = template.Library()
@register.simple_tag()
def model_desc(obj):
if obj.__doc__:
return mark_safe('<p>{}</p>'.format(obj.__doc__))
return ''
現在,您的項目目錄中(其中manage.py
生活)創建一個結構是這樣的:
project/
project/
project stuff here, i.e wsgi.py, settings etc
myapp/
myapp stuff here, i.e models, views etc
templates/
admin/
change_list.html
manage.py
裏面的change_list.html
添加這些:
{% extends 'admin/change_list.html' %}
{% load yourapp_tags %}
{# block.super will print the "Select <Model> to change" h1 title #}
{# The model_desc template tag is the one you created and prints the docstring of the given model #}
{% block content_title %}{{ block.super }}<br>{% model_desc cl.model %}{% endblock %}
以下是截圖:
[更新]:我有seen in the source當沒有指定docstring
,Django會生成一個適合你形式如下:ModelName(model_field_name1, model_field_name2, ...)
。如果你不想這樣做,只需做到這一點:
class MyModelWithoutDocstring(models.Model):
# model fields here
MyModelWithoutDocstring.__doc__ = '' # "reset" the __doc__ on this model.
在第2步中,您要求用戶創建全局change_list.html。但是每個模型都會有自己的change_list.html。你可能想糾正這一點。 –
這取決於你想要如何模塊化。我決定爲所有模型全局添加它,以在'change_list'頁面中顯示它們的'docstring'。 –
以下答案是否適用於你的情況? –
是的,我在HTML方面做了一些改變。只需將templatetag放在不同的行中,以確保右側的按鈕對齊不會受到影響。謝謝! –