Skip to main content

[![Build Status](https://secure.travis-ci.org/gbezyuk/django-ordered-model-grappelli.png?branch=master)](https://travis-ci.org/gbezyuk/django-ordered-model-grappelli)

This is a fork of original django-ordered-model by bfirsh. Text bellow is left unchanged.

django-ordered-model allows models to be ordered and provides a simple admin interface for reordering them.

Based on https://djangosnippets.org/snippets/998/ and https://djangosnippets.org/snippets/259/

Requires:

  • Django >=1.5

Installation

$ python setup.py install

Usage

Add ordered_model to your SETTINGS.INSTALLED_APPS.

Inherit your model from OrderedModel to make it ordered:

from django.db import models from ordered_model.models import OrderedModel

class Item(OrderedModel):

name = models.CharField(max_length=100)

class Meta(OrderedModel.Meta):

pass

Model instances now have a set of methods to move them relative to each other. To demonstrate those methods we create two instances of Item:

foo = Item.objects.create(name=”Foo”) bar = Item.objects.create(name=”Bar”)

### Swap positions

foo.swap(bar)

This swaps the position of two objects.

### Move position up on position

foo.up() foo.down()

Moving an object up or down just makes it swap its position with the neighouring object directly above of below depending on the direction.

### Move to arbitrary position

foo.to(12) bar.to(13)

Move the object to an arbitrary position in the stack. This essentially sets the order value to the specified integer. Objects between the original and the new position get their order value increased or decreased according to the direction of the move.

### Move object above or below reference

foo.above(bar) foo.below(bar)

Move the object directly above or below the reference object, increasing or decreasing the order value for all objects between the two, depending on the direction of the move.

### Move to top of stack

foo.top()

This sets the order value to the lowest value found in the stack and increases the order value of all objects that were above the moved object by one.

### Move to bottom of stack

foo.bottom()

This sets the order value to the highest value found in the stack and decreases the order value of all objects that were below the moved object by one.

## Subset Ordering

In some cases, ordering objects is required only on a subset of objects. For example, an application that manages contact lists for users, in a many-to-one/many relationship, would like to allow each user to order their contacts regardless of how other users choose their order. This option is supported via the order_with_respect_to parameter.

A simple example might look like so:

class Contact(OrderedModel):

user = models.ForeignKey(User) phone = models.CharField() order_with_respect_to = ‘user’

Admin integration

To add arrows in the admin change list page to do reordering, you can use the OrderedModelAdmin and the move_up_down_links field:

from django.contrib import admin from ordered_model.admin import OrderedModelAdmin from models import Item

class ItemAdmin(OrderedModelAdmin):

list_display = (‘name’, ‘move_up_down_links’)

admin.site.register(Item, ItemAdmin)

Test suite

Requires Docker.

$ script/test

Release files for django-ordered-model-grappelli 1.0.1

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-ordered-model-grappelli 1.0.1
File Size Uploaded
django-ordered-model-grappelli-1.0.1.tar.gz 9.8 kB Details

Release files / django-ordered-model-grappelli-1.0.1.tar.gz

Download URL django-ordered-model-grappelli-1.0.1.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f1fb65e53a32e5e1d1ff91a529e2114da3b943a517c2844a30be171e4649cf2
BLAKE2b-256 checksum
How to use checksums
1ac0ed249745335bea7dcfc74d4a10007b338cbf2ffd9f17ec38e6e2c5eb10fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

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