Skip to main content

Easily reorder the order of items within the django admin backend.

Project description

Welcome to django-reorder-items-widget

Tests

Description

Reorder your items by simply dragging them to their new position. This works fine within django admin's changelists or inline model forms.

All you need to do is to use an editable index field with the famous ReorderItemsWidget of this app.

Installation

Install via pip:

pip install django-reorder-items-widget

Setup

Add reorder_items_widget to your INSTALLED_APPS:

INSTALLED_APPS = [
    'reorder_items_widget',
    ...
]

Add a index field to your model:

class Item(models.Model):
    index = models.PositiveSmallIntegerField()
    ...
    class Meta:
        ordering = ('index',)

Using the ReorderItemsWidget with your index field only makes sense in result lists or inline modeladmin forms. A simple way to put the widget in place is a custom model form:

class ReorderItemForm(forms.ModelForm):
    class Meta:
        widgets={'index': ReorderItemsWidget()}

Now you can use this form with your changelist by overwriting the get_changelist_form method of your model admin:

class BaseItemAdmin(admin.ModelAdmin):
    list_editable = ("index",)
    list_display = ("index", ...)

    def get_changelist_form(self, request, **kwargs):
        kwargs.setdefault('form', ReorderItemForm)
        return super().get_changelist_form(request, **kwargs)

NOTE: Mind that your index field must be editable.

To use the widget with your inline modeladmin simple at your form to the TabularInline class:

class ItemInline(admin.TabularInline):
    form = ReorderItemForm
    fields = ("index", ...)
    ...

That's it.

Caveats

The widget will always number your items sequentially. Reordering items in a filtered list might be have unexpected results. Paging however should not be a problem since indexes are updated using the lowest one as base.

General considerations on switching index values

There is a general problem with switching values on a unique index field: In mysql like databases you will run into a constraint violation - even if you update all items in a single update transaction.

To work around this you can either obmit the unique constraint. Or implement a complex saving logic like saving changed indexes as negative values first and update them to their positiv counterpart afterwards.

Contribute

Feedback, feature requests, bug reports or pull requests are most welcome. Just use the common github infrastructure.

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_reorder_items_widget-1.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

django_reorder_items_widget-1.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file django_reorder_items_widget-1.1.0.tar.gz.

File metadata

File hashes

Hashes for django_reorder_items_widget-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e806a5d8440b6f4e58eb6de006745b53a57929b1f40437362926a01700a6cd7b
MD5 62ce14d18d5fc356fff3b7343f636f9c
BLAKE2b-256 819d1bfb4cffaa11f4e34a97f78f0d332920f48eeca5fea4a45ad07c39a7d4b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_reorder_items_widget-1.1.0.tar.gz:

Publisher: publish.yml on thomst/django-reorder-items-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_reorder_items_widget-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_reorder_items_widget-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f9ba2dfdf1f3124222a0c219350dd0d2636cd6e776f859963554d0a3a4ef61c
MD5 a8b23496ed297938d5f1f8fd241270e7
BLAKE2b-256 81350baa109623825b85d53f689e6eda4844c9ce297eb603c56500bd1d04fc36

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_reorder_items_widget-1.1.0-py3-none-any.whl:

Publisher: publish.yml on thomst/django-reorder-items-widget

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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