Django admin tips and tricks - Part 1

Django admin tips and tricks - Part 1

Django admin tips and tricks

Here you go:

How to make a column editable in the Django admin change list view in a Model?

We can make a column editable in the Django admin using an attribute list_editable as given in the below example. This can be helpful if you want to bulk update rows quickly, without opening the details of rows one by one in the change list view.

# app_name/admin.py

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = ("username", "is_active", "email")
    list_editable = ("is_active",)  # `is_active` will be editable in all the rows

How to add custom JavaScript/CSS files in Django model admin form in a Model?

If you want to run some JavaScript/CSS file in a Django model admin form, here is how you do it. The JavaScript/CSS file should be in the static files folder.

# app_name/admin.py

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    ...
    class Media:
        css = {"all": ("pretty.css",)} # Here "all" denotes the html media attribute
        js = (
            "animation.js",
            "action.js",
        )

How to unregister a native or third party Model (Group etc.) in Django admin?

Sometimes you do not want to show Django's Group model or a model from a third party Django library which you have installed. Here is how you can unregister/hide it from the Django admin panel.

# Unregister the django Group model
from django.contrib.auth.models import Group
admin.site.unregister(Group)

# Unregiser a third party model
from thirdparty.models import ThirdPartyModel
admin.site.unregister(ThirdPartyModel)

How to reset the Django admin password through the command line?

We are humans, If you have forgotten the password of the Django admin panel, you can reset it by running this command.

python manage.py changepassword <username>

Note: username is the admin’s username.

How to show extra columns in the Django admin change list view in a Model?

When you register a model in the Django admin, it will by default show a single column having the text of __str__ the method defined in the model class, let's show some more fields of a Django model using the list_display class attribute.

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    list_display = (
        "username",
        "email",
        "first_name",
        "last_name",
        "is_active",
        "is_staff",
        "is_superuser",
    )

This is it for this blog, let's meet in the next one.

Any thoughts? Write it down in the comments.

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

Did you find this article valuable?

Support Dev.Junction by becoming a sponsor. Any amount is appreciated!