mozman2
mozman2

Reputation: 929

Django admin model description list

When I am in something like /admin/

I see a list of all my apps and models (with add and change buttons)

I would like to add a description for each model below the model name, is that possible

I can see how to add a description on the list change list view

Model description in django-admin

Is it possible to do something similar when I see my list of models

Thanks

Grant

Upvotes: 1

Views: 337

Answers (2)

mozman2
mozman2

Reputation: 929

Thanks for the direction

I read up on this and this is what I needed

Ordering admin.ModelAdmin objects

I tweaked it to call a template tag {% model_desc_from_string app model.object_name %}

Calling this

def class_for_name(module_name, class_name):
    import importlib
    # load the module, will raise ImportError if module cannot be loaded
    m = importlib.import_module(module_name)
    # get the class, will raise AttributeError if class cannot be found
    c = getattr(m, class_name)
    return c

@register.simple_tag()
def model_desc_from_string(module_name, class_name):
    import inspect
    try:
        full_module_name = module_name['app_label'] + ".models"
        loaded_class = class_for_name(full_module_name, class_name)
        return inspect.getdoc(loaded_class)
    except:
        return "none"

It get the doc string of teh class and then adds that.

This code needs tidying up, but you should get the idea

Thanks

Grant

Upvotes: 0

Eliakin Costa
Eliakin Costa

Reputation: 960

As said before, you would need to implement your own root template. You can check how to do this on the docs.

Upvotes: 0

Related Questions