Skip to main content

Django better admin ArrayField

image

Actions Status

image

Better ArrayField widget for admin

Supported Python versions: Python 3.5 Python 3.6 Python 3.7 Python 3.8

Supported Django versions: 2.0, 2.1, 2.2, 3.0, 3.1

might work with different django/python versions as well but I did not test that.

It changes comma separated widget to list based in admin panel.

Before: Alt text

After: Alt text

Quickstart

Install Django better admin ArrayField:

pip install django-better-admin-arrayfield

Add it to your `INSTALLED_APPS`:

INSTALLED_APPS = (
    ...
    'django_better_admin_arrayfield',
    ...
)

Usage

django_better_admin_arrayfield.models.fields.ArrayField is a drop-in replacement for standard Django ArrayField.

Import it like below and use it in your model class definition.

from django_better_admin_arrayfield.models.fields import ArrayField

Import DynamicArrayMixin like below

from django_better_admin_arrayfield.admin.mixins import DynamicArrayMixin

In your admin class add DynamicArrayMixin: ...

class MyModelAdmin(admin.ModelAdmin, DynamicArrayMixin):

That's it.

Custom subwidget

By default the subwidget (the one used for each item in the array) will be TextInput. If you want something else, you can use your own specifying it in the formfield_overrides of your Admin model:

class MyWidget(DynamicArrayWidget):
    def __init__(self, *args, **kwargs):
        kwargs['subwidget_form'] = MyForm
        super().__init__(*args, **kwargs)

class MyModelAdmin(models.ModelAdmin, DynamicArrayMixin):
    ...
    formfield_overrides = {
        DynamicArrayField: {'widget': MyWidget},
    }

If you wanted to have Textarea as the subwidget, you can simply use the included drop-in widget replacement:

from django_better_admin_arrayfield.forms.widgets import DynamicArrayTextareaWidget

class MyModelAdmin(models.ModelAdmin, DynamicArrayMixin):
    ...
    formfield_overrides = {
        DynamicArrayField: {'widget': DynamicArrayTextareaWidget},
    }

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

Pre-commit hooks

Install pre-commit black hook

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_dev.txt
(myenv) $ pre-commit install

Credits

Inspired by: https://stackoverflow.com/a/49370480/4638248

Tools used in rendering this package:

History

1.4.2 (2020-12-08)

  • Adjust template to better match django style

1.4.1 (2020-12-08)

  • Allow submitting empty array field

1.4.0 (2020-10-04)

  • allow choosing subwidget for DynamicArrayWidget

1.3.0 (2020-07-09)

  • Handle default values in form field

1.2.1 (2020-07-09)

  • Fix tests requirements

1.2.0 (2020-07-09)

  • handle default values in model fields

1.1.0 (2020-04-28)

  • Add spanish translations

1.0.7 (2020-04-27)

  • Add possibility to i18n strings

1.0.6 (2020-04-15)

  • Remove debugging print statements
  • use default_app_config for easier integration
  • Support dynamically-added inline forms

1.0.5 (2019-12-30)

  • Add python 3.8 and Django 3.0 to tests

1.0.4 (2019-09-02)

  • Can add item after removing everything from the list

1.0.3 (2019-09-02)

  • Can add item after removing everything from the list
  • Do not call static at startup time

1.0.2 (2019-04-03)

  • If field is required empty list raises ValidationError on clean.

1.0.1 (2019-02-23)

  • Empty list is no longer recognized as changed.

1.0.0 (2019-02-21)

  • First release on PyPI.

Metadata

Release files for django-better-admin-arrayfield 1.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-better-admin-arrayfield 1.4.2
File Size Uploaded
django-better-admin-arrayfield-1.4.2.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-better-admin-arrayfield 1.4.2
File Interpreter ABI Platform
django_better_admin_arrayfield-1.4.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 27.8 kB

Release files / django-better-admin-arrayfield-1.4.2.tar.gz

Download URL django-better-admin-arrayfield-1.4.2.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b45423e51bbc0aa31ef658248c058ca8b533a541be4dee9fb8bcd059f8a10a58
BLAKE2b-256 checksum
How to use checksums
354fe8bdf86d5bba2622d585bec983a0b59f373f4ed00eb948a6907ba35e5585
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

Release files / django_better_admin_arrayfield-1.4.2-py2.py3-none-any.whl

Download URL django_better_admin_arrayfield-1.4.2-py2.py3-none-any.whl
Size 13.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
bfeaa0fa8210a7ea95ee996a6caaa59ecd0c923269f573e6d8319c28dcac5c88
BLAKE2b-256 checksum
How to use checksums
eb758fc6cfc73c456283c89a80bd8319df61aabcd273cfa37697bc31a00e387b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.4.2 This release

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

1 release file

1.0.4

2 release files

1.0.3

1 release file

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page