django-ordered-model
django-ordered-model is a Django library that enables models to be ordered, providing a simple admin interface for reordering them. It is actively maintained, with frequent updates, including a recent alpha release for version 3.8.0. The current stable version is 3.7.4.
Warnings
- breaking Subclasses of `OrderedModelBase` (including `OrderedModel`) must explicitly specify `Meta.ordering`. Failing to do so will emit a system `Check` failure since version 3.7.
- breaking Since version 3.7, updating fields that are part of `order_with_respect_to` now correctly adjusts the ordering. This is a behavioral change that might impact existing logic if it relied on the previous, potentially incorrect, behavior.
- gotcha If you use a custom `ModelManager` or `QuerySet` with an `OrderedModel`, they must inherit from `OrderedModelManager` or `OrderedModelQuerySet` respectively. Failing to do so will raise Django `Check` errors (E003/E004). As of 3.7.4, this is relaxed to a warning if the manager returns the correct queryset.
- gotcha When using `order_with_respect_to`, the library implicitly sets an internal `_order` field for ordering. Therefore, `order_with_respect_to` and `Meta.ordering` cannot be used simultaneously in the same model's `Meta` class and will raise a Django error.
- gotcha Methods like `to()`, `up()`, `down()`, `top()`, `bottom()`, and `delete()` use Django's `update()` method for performance when shifting other objects. This means custom `save()` logic or `DateTimeField(auto_now=True)` on *other shifted objects* will not be called. For the target object itself, `save()` is called if not directly manipulating `order` field.
- gotcha Django's `ManyToManyField` does not inherently respect `Meta.ordering` on intermediate (through) models when querying the 'members' queryset (e.g., `pizza.toppings.all()`).
Install
-
pip install django-ordered-model
Imports
- OrderedModel
from ordered_model.models import OrderedModel
- OrderedModelBase
from ordered_model.models import OrderedModelBase
- OrderedModelManager
from ordered_model.models import OrderedModelManager
- OrderedModelQuerySet
from ordered_model.models import OrderedModelQuerySet
- OrderedModelAdmin
from ordered_model.admin import OrderedModelAdmin
- OrderedModelSerializer
from ordered_model.serializers import OrderedModelSerializer
- OrderedManyToManyField
from ordered_model.fields import OrderedManyToManyField
- OrderedTabularInline
from ordered_model.admin import OrderedTabularInline
Quickstart
from django.db import models
from django.contrib import admin
from ordered_model.models import OrderedModel
from ordered_model.admin import OrderedModelAdmin
# models.py
class Item(OrderedModel):
name = models.CharField(max_length=100)
class Meta(OrderedModel.Meta):
verbose_name = "Item"
verbose_name_plural = "Items"
# admin.py
class ItemAdmin(OrderedModelAdmin):
list_display = ('name', 'move_up_down_links')
admin.site.register(Item, ItemAdmin)