Skip to main content

A Django extension that allows users to reorder list view columns and toggle their visibility

Project description

Django Admin FlexList

A Django package that enhances the admin interface by providing customizable list views. This package enables users to customize the Django admin list view by adding an "Edit layout" button to the page. When clicked, it opens a dialog form that allows users to:

  • Toggle the visibility of list columns
  • Change the order of columns
  • Save their preferred layout for future use

demo

Setup

1. Installation

To install the package, run:

$ pip install django-admin-flexlist

2. Install 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. Extend the admin class

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

Limitations

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-0.1.1.tar.gz (16.7 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-0.1.1-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_admin_flexlist-0.1.1.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for django_admin_flexlist-0.1.1.tar.gz
Algorithm Hash digest
SHA256 764cbd012c1320b05e960e98f332edc6552b74c1886a68b60bf28dc4e24f4938
MD5 9612c74464d924e7a55224f015e48bbf
BLAKE2b-256 f530ae0e6a5f572d9c3a358b4f178156c37215c9c12c631450e81c8de6768f83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_admin_flexlist-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7cb66fba15e051815808a10ed9358776215142418dd07d6b6b873f2a69920e8a
MD5 a310a0f9976c782d5a31eb570cc49870
BLAKE2b-256 e31dc0fd0143ce765383eee2918b7a7800165c1f57eb8030e5d4d4b8c5f5ce2f

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