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. So reordering a filtered list of items might have unexpected results. Paging however should not be an issue since indexes are updated using the index of the first list item as base.

General considerations on switching index values

There is a general issue 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 using the negative counterparts of your indexes in a preceding update round and finally switch those values back to their positive equivalent.

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.1.tar.gz (7.3 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.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for django_reorder_items_widget-1.1.1.tar.gz
Algorithm Hash digest
SHA256 44f21733dfd04e2f55a0466912f60a35dea21b81d5fd135da5b71ab915a7d3c9
MD5 80ccc476167c14d7e8155fe19f4c62d7
BLAKE2b-256 91d003570c7343125597d908a1d350d45796299d74ec01ec11479a3e70297862

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for django_reorder_items_widget-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e969a2352999d34660b8efb184cacf39a5059294942c0978adc7bc1950661c0
MD5 a2323b2e7764b12160df64e8dd94d700
BLAKE2b-256 6106f5e3721107d36ae1ac419394b0fd9f0c7de3313e08d1507c278273cb7ba3

See more details on using hashes here.

Provenance

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