Skip to main content

A Django extension that allows users to reorder and toggle visibility of admin list view, app index, and dashboard elements

Project description

Django Admin FlexList

Published on Django Packages

A Django package that enables users to customize the admin list view, app index, and dashboard by adding an "Edit layout" button to the page. When clicked, it opens a dialog form that allows users to:

  • Toggle the visibility of columns, models, or app sections
  • Change their order
  • Save their preferred layout for future use

demo

Setup

1. Install the package

To install the package, run:

$ pip install django-admin-flexlist

2. Install the app

Add "django_admin_flexlist" to INSTALLED_APPS in your project's settings.py file:

INSTALLED_APPS = [
    # ...
    "django_admin_flexlist",
    # ...
]

This allows Django to find the package's migrations and templates.

3. Apply migrations

Django Admin FlexList stores each user's layout preferences in the database. To create the necessary table, run:

$ python manage.py migrate

4. Include URLs

The package adds endpoints so the JavaScript on the custom template can load the user's preferences into the dialog form and then save the changes to the DB model. To enable those, add the following to your project's urls.py file:

urlpatterns = [
    # ...
    path("", include("django_admin_flexlist.urls")),
    # ...
]

5. Integrate functionality

The package supports customizing two types of Django admin pages: list views and index pages. Index pages include both the main dashboard (showing apps and models) and app-specific dashboards (showing models from a selected app). You can choose to integrate either or both.

5.1 Integrate list view functionality

The customization functionality is introduced by the FlexListAdmin class. It mainly implements two changes:

  1. Intercepts the get_list_display method to apply the user's custom changes
  2. Uses a custom change list template that allows the user to change the layout

To use it, extend your admin class as follows:

from django_admin_flexlist import FlexListAdmin

class YourAdmin(FlexListAdmin):
    pass

Notice that you can replace admin.ModelAdmin with the FlexListAdmin class, since the latter already extends that Django class.

You don't need to implement the get_list_display method for this feature to work. If you do override it, the customization should still work without any additional changes.

In case your admin class overrides change_list_template, you can keep the "Edit layout" feature by extending your custom template as follows:

{% extends "django_admin_flexlist/change_list.html" %}

In case your custom template already extends a template other than "admin/change_list.html", you can still keep the feature by adding the following blocks to your file:

{% block extrahead %}
{{ block.super }}
{% include "django_admin_flexlist/change_list_head.html" %}
{% endblock %}

{% block object-tools-items %}
{% include "django_admin_flexlist/change_list_actions.html" %}
{{ block.super }}
{% endblock %}

{% block content %}
{{ block.super }}
{% include "django_admin_flexlist/change_list_content.html" %}
{% endblock %}

5.2 Integrate index page functionality

To enable customization on index pages, replace "django.contrib.admin" in settings.py as follows:

INSTALLED_APPS = [
    # ...
    "django_admin_flexlist.FlexListAdminConfig",  # replaces "django.contrib.admin"
    # ...
]

This swaps Django's default admin site class with one from the package, adding an "Edit layout" button and support for customizing the app and model lists. All other admin features remain unchanged.

6. Overrides

6.1 Custom admin site

If you're already using a custom admin site class, extend it like this:

from django_admin_flexlist import FlexListAdminConfig, FlexListAdminSite

class YourAdminSite(FlexListAdminSite):
    pass


class YourAdminConfig(FlexListAdminConfig):
    default_site = "your_project.path.to.YourAdminSite"

Then replace "django.contrib.admin" in INSTALLED_APPS with "your_project.path.to.YourAdminConfig".

This package supports custom admin sites by replacing Django's default admin site. For list view customization, models should be registered on that default admin site.

6.2 Custom index templates

If you're overriding "admin/index.html" or "admin/app_index.html", you can keep this feature by including the following blocks:

  • In your custom index template:
{% block extrastyle %}
{{ block.super }}
{% include "django_admin_flexlist/index_head.html" %}
{% endblock %}

{% block content %}
{{ block.super }}
{% include "django_admin_flexlist/index_actions.html" %}
{% include "django_admin_flexlist/index_content.html" %}
{% endblock %}
  • In your custom app index template:
{% block extrastyle %}
{{ block.super }}
{% include "django_admin_flexlist/app_index_head.html" %}
{% endblock %}

{% block content %}
{{ block.super }}
{% include "django_admin_flexlist/app_index_actions.html" %}
{% include "django_admin_flexlist/app_index_content.html" %}
{% endblock %}

If you use the same custom template for both the index page and the app index page, include the following blocks instead:

{% block extrastyle %}
{{ block.super }}
{% if app_label %}
{% include "django_admin_flexlist/app_index_head.html" %}
{% else %}
{% include "django_admin_flexlist/index_head.html" %}
{% endif %}
{% endblock %}

{% block content %}
{{ block.super }}
{% if app_label %}
{% include "django_admin_flexlist/app_index_actions.html" %}
{% include "django_admin_flexlist/app_index_content.html" %}
{% else %}
{% include "django_admin_flexlist/index_actions.html" %}
{% include "django_admin_flexlist/index_content.html" %}
{% endif %}
{% endblock %}

6.3 Custom styling

The colors used by the components are defined in django_admin_flexlist/css/colors.css. You can override those values with your own, as seen in the demo project's base/static/base/css/colors.css file, and adjust your templates to use your custom CSS file, as seen in templates/admin/base_site.html.

Limitations

For list views, the implementation expects fields defined in list_display or get_list_display to be a list or tuple of strings. This simplifies serializing and deserializing changes saved to the DB model. As a result, the following Django implementation of a field isn't supported:

@admin.display(description="Name")
def upper_case_name(obj):
    return f"{obj.first_name} {obj.last_name}".upper()

class PersonAdmin(admin.ModelAdmin):
    list_display = [upper_case_name]

Demo project

To try out the package, you can run the included demo project. Make sure you have tox installed. Then:

  1. Clone this repository
  2. Run the demo project with:
    $ tox -e py310-django42 -- runserver
    
    (Replace py310-django42 with your desired Python and Django versions - see tox.ini for supported combinations)

This will:

  • Spin up a Django app with the specified version
  • Create a new SQLite database
  • Populate it with two demo users:
    • Username: "jon.doe", Password: "jon.doe"
    • Username: "jane.doe", Password: "jane.doe"

The supported Python and Django versions can be found in the tox.ini file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_flexlist-1.0.5.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_flexlist-1.0.5-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_flexlist-1.0.5.tar.gz.

File metadata

  • Download URL: django_admin_flexlist-1.0.5.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for django_admin_flexlist-1.0.5.tar.gz
Algorithm Hash digest
SHA256 6e931e8701a4be62c296be8f1d403e11938412b1ac01832d7c313f2800f67d97
MD5 082d22845e4db77b664bf234cc468cb3
BLAKE2b-256 2b6c994cdec220d04fcc73382132d78f8f14487b1b4ca169613ead6ba5ff396e

See more details on using hashes here.

File details

Details for the file django_admin_flexlist-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_flexlist-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 949afcebd1372563775bb33d03965f606984a4f9066feb15c6c15460bff5badb
MD5 2a64dbca0d34aec057a1e6372bad73d3
BLAKE2b-256 d15d6750fd95f1e21298c97d551393a7a898b27daf804b2bc21924e92b4e721e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page