Understanding the Django admin

Thank you for the insights @KenWhitesell @Eleuthar! It is good to know that I am not the only one struggling with Django Admin :slight_smile:

If I could choose, I would attempt to rebuild the project from scratch(as the authors of Django recommend).

The problem is, Django Admin IS the UI for the current project and it will be. It is not for me to decide at this point, too much time in invested already. As someone who has recently been assigned to this project, I have to go with it.

So I will have to be making changes and I will need the tools for that.

Sadly currently I did not find a guide that provides you the tools. Perhaps we could turn this thread into one, just to start? It would be helpful for me and for others reading this thread.

Tools to understand/grasp the Django admin:

  • When we install Django package into our env, we can navigate to it’s source code. Either over vscode by clicking Ctrl+Click or by physically navigating to the location of the django admin source files in django/contrib/admin and looking through the source code there. Best if you understand the OOP(Object-oriented programming in python) first.

  • While in django shell, do an import - from yourapp.models import YourModelName, then run print(YourModelName._meta.__dict__). This will print all the “opts” of the Model? Did not use this yet, but good to know. Have to look into opts(options?) and understand what they hold and how they can be useful. Thanks @Eleuthar for this tip.

  • You can "rip off " the inlines off the bottom of the change/add page by using a few lines of Javascript(there is no default built in django option for that, inlines are always placed at the bottom of the page). Javascript example :

    const selected_div = document.querySelector('div.field-car_type');
    const selected_div_parent = selected_div.parentElement.parentElement;
    const field_to_move = document.getElementById('hello_value-group');
    selected_div_parent.insertAdjacentElement('afterend', field_to_move);
    

    Also, get good at using your browser console. Thanks @Eleuthar for javascript tip.

  • Get good at overriding and extending templates. You will want to do it if at some point you will want to delete/add desired elements. For example to hide the “history” button in Django admin, I found out that overriding the default “change_form.html” template and getting rid of {% change_form_object_tools %} was a clean way to get rid of the button. This removes history button for all the models. If I want to remove the button in a particular model, in admin.py I have to use change_form_template = "your_template_name". Found this out by reading the source code( First step :wink: ) .

I will come to this place to add new things I learn. If you have some - feel free to write yours! (I am sure you have more tips, tricks and tools). However small or big, lets write them here :slight_smile:

Lets make this thread heaven for Django admin developers :slight_smile:

1 Like