Django Better Admin ArrayField

1.4.2 · maintenance · verified Thu Apr 16

django-better-admin-arrayfield provides an enhanced widget for Django's `ArrayField` within the Django administration interface. It replaces the default comma-separated input with a more user-friendly list-based interface, allowing for dynamic addition and removal of items. The library is currently at version 1.4.2, last released in December 2020, and explicitly supports Python 3.5-3.8 and Django 2.0-3.1.

Common errors

Warnings

Install

Imports

Quickstart

To quickly integrate `django-better-admin-arrayfield`, add `django_better_admin_arrayfield` to your `INSTALLED_APPS` in `settings.py`. In your `models.py`, use `ArrayField` from `django_better_admin_arrayfield.models.fields`. Finally, in `admin.py`, apply the `DynamicArrayMixin` to your `ModelAdmin` class. This setup replaces the default `ArrayField` widget with the improved list-based interface in the Django admin.

import os
import django
from django.conf import settings
from django.core.management import call_command

# Minimal Django settings for quickstart
if not settings.configured:
    settings.configure(
        DEBUG=True,
        INSTALLED_APPS=[
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            'django.contrib.postgres',
            'django_better_admin_arrayfield',
            'my_app',
        ],
        DATABASES={
            'default': {
                'ENGINE': 'django.db.backends.sqlite3',
                'NAME': ':memory:',
            }
        },
        TEMPLATES=[
            {
                'BACKEND': 'django.template.backends.django.DjangoTemplates',
                'APP_DIRS': True,
                'OPTIONS': {
                    'context_processors': [
                        'django.template.context_processors.debug',
                        'django.template.context_processors.request',
                        'django.contrib.auth.context_processors.auth',
                        'django.contrib.messages.context_processors.messages',
                    ],
                },
            },
        ],
        STATIC_URL='/static/',
        SECRET_KEY=os.environ.get('DJANGO_SECRET_KEY', 'a-very-secret-key-for-testing-only'),
        MIDDLEWARE_CLASSES=[
            'django.contrib.sessions.middleware.SessionMiddleware',
            'django.middleware.common.CommonMiddleware',
            'django.middleware.csrf.CsrfViewMiddleware',
            'django.contrib.auth.middleware.AuthenticationMiddleware',
            'django.contrib.messages.middleware.MessageMiddleware',
            'django.middleware.clickjacking.XFrameOptionsMiddleware',
        ],
        ROOT_URLCONF='my_app.urls',
    )
django.setup()

# my_app/models.py
from django.db import models
from django_better_admin_arrayfield.models.fields import ArrayField

class TaggedItem(models.Model):
    name = models.CharField(max_length=255)
    tags = ArrayField(models.CharField(max_length=100), blank=True, default=list)

    def __str__(self):
        return self.name

# my_app/admin.py
from django.contrib import admin
from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin
from .models import TaggedItem

@admin.register(TaggedItem)
class TaggedItemAdmin(admin.ModelAdmin, DynamicArrayMixin):
    list_display = ('name', 'tags')
    # Example of custom subwidget if needed
    # from django_better_admin_arrayfield.forms.widgets import DynamicArrayTextareaWidget
    # formfield_overrides = {
    #    ArrayField: {'widget': DynamicArrayTextareaWidget},
    # }

# my_app/urls.py
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

# Example of running migrations and creating a superuser (for testing admin access)
if __name__ == '__main__':
    try:
        call_command('makemigrations', 'my_app')
        call_command('migrate')
        print("Django setup complete. Access admin at /admin/")
    except Exception as e:
        print(f"Error during Django setup: {e}")

    # You can now run the Django development server using: python manage.py runserver
    # (assuming you have a manage.py set up to use these settings)

view raw JSON →