Model description in django-admin

2024/10/12 4:33:27

Is it possible to put a model description or description on the list display page of a certain model in django-admin?

I'm talking about something like when you click a model name link on the homepage of django-admin and when you go to the list display page of that model. There will be a description written on top of the table. Something like

"This model is used to log all accounts that will be fetched by our scrape.... And so on"

Something like that, is it possible?

Answer

That would be a very nice feature to be added in the core of Django's admin. Until then here's a quick walkthrough to your question.

Let's assume that you want to print the docstring of each model, like this:

class MyModel(models.Model):"""I wanna get printed in the Admin!"""# model fields here

So, you want to print it in the change_list page. Good.

  1. Create a custom template tag (either within your app or create another app that will hold the global template tags/filters) like this:

    from django import template
    from django.utils.html import mark_saferegister = template.Library()@register.simple_tag()
    def model_desc(obj):if obj.__doc__:return mark_safe('<p>{}</p>'.format(obj.__doc__))return ''
    
  2. Now, inside your project directory (where manage.py lives) create a structure like this:

    project/project/project stuff here, i.e wsgi.py, settings etcmyapp/myapp stuff here, i.e models, views etctemplates/admin/change_list.htmlmanage.py
    
  3. Inside the change_list.html add these:

    {% 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 %}
    

Here's a screenshot:

Model docstring in Django admin

[UPDATE]: I have seen in the source that when there is no docstring specified, Django will generate one for you in the following form: ModelName(model_field_name1, model_field_name2, ...). If you do not want that, simply do this:

class MyModelWithoutDocstring(models.Model):# model fields hereMyModelWithoutDocstring.__doc__ = ''  # "reset" the __doc__ on this model.
https://en.xdnf.cn/q/69694.html

Related Q&A

Print underscore separated integer

Since python3.6, you can use underscore to separate digits of an integer. For examplex = 1_000_000 print(x) #1000000This feature was added to easily read numbers with many digits and I found it very u…

What does (numpy) __array_wrap__ do?

I am diving into the SciPy LinAlg module for the first time, and I saw this function:def _makearray(a):new = asarray(a)wrap = getattr(a, "__array_prepare__", new.__array_wrap__)return new, wr…

SqlAlchemy TIMESTAMP on update extra

I am using SqlAlchemy on python3.4.3 to manage a MySQL database. I was creating a table with:from datetime import datetimefrom sqlalchemy import Column, text, create_engine from sqlalchemy.types import…

Is it possible to pass a dictionary with extraneous elements to a Django object.create method?

I am aware that when using MyModel.objects.create in Django, it is possible to pass in a dictionary with keys which correspond to the model fields in MyModel. This is explained in another question here…

When should I use varargs in designing a Python API?

Is there a good rule of thumb as to when you should prefer varargs function signatures in your API over passing an iterable to a function? ("varargs" being short for "variadic" or …

PyPDF2 wont extract all text from PDF

Im trying to extract text from a PDF (https://www.sec.gov/litigation/admin/2015/34-76574.pdf) using PyPDF2, and the only result Im getting is the following string:bHere is my code:import PyPDF2 import …

Python 3.4 decode bytes

I am trying to write a file in python, and I cant find a way to decode a byte object before writing the file, basically, I am trying to decode this bytes string:Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s…

No module named unusual_prefix_*

I tried to run the Python Operator Example in my Airflow installation. The installation has deployed webserver, scheduler and worker on the same machine and runs with no complaints for all non-PytohnOp…

Python: Variables are still accessible if defined in try or if? [duplicate]

This question already has answers here:Short description of the scoping rules(9 answers)Closed last year.Im a Python beginner and I am from C/C++ background. Im using Python 2.7.I read this article: A …

Networkx Traveling Salesman Problem (TSP)

I would like to know if there is a function in NetworkX to solve the TSP? I can not find it. Am I missing something? I know its an NP hard problem but there should be some approximate solutions right