Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

python - Adding model-wide help text to a django model's admin form

In my django app, I would like to be able to add customized help text to the admin change form for some of my models. Note I'm not talking about the field specific help_text attribute that I can set on individual fields. For example, at the top of the change form for My_Model in My_App I'd like to be able to add some HTML that says "For additional information about My Model, see http://example.com" in order to provide a link to an internal documentation wiki.

Is there any simple way of accomplishing this, or do I need to create a custom admin form for the model? If so, can you give me an example of how I would do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the admin's fieldsets:

class MyAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('first', 'second', 'etc'),
            'description': "This is a set of fields group into a fieldset."
        }),
    )
    # Other admin settings go here...

You can have multiple fieldsets in an admin. Each can have its own title (replace the None above with the title). You can also add 'classes': ('collapse',), to a fieldset to have it start out collapsed (the wide class makes the data fields wider, and other class names mean whatever your CSS says they do).

Be careful: the description string is considered safe, so don't put any uncleaned data in there. This is done so you can put markup in there as needed (like your link), however, block formatting (like <ul> lists) will probably look wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...