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.0.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.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_reorder_items_widget-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9446f947c73fe5efdbb33229b0013cf6a1652706b3e1e4d2fa5024886efa5bb8
MD5 7098c7857bf24ac6c7c7faceb50f8dbf
BLAKE2b-256 da8e30b4fe31442e5e06f7da0f3b54a672fdeed9629f4710f557f59c4417c81e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_reorder_items_widget-1.0.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.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_reorder_items_widget-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e34dfd1eb72b1b1cdc11380b65d72e73a8655d195848134efbfb9e1416568ac
MD5 6e9398aac73eaf656ceceea709683a96
BLAKE2b-256 2452938a1109e784e64132dbf909b7db53a07c82c15e355c20ef54496d4dbe89

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_reorder_items_widget-1.0.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