This module provides
PositionField, a model field for Django that allows
instances of a model to be sorted by a user-specified position. Conceptually,
the field works like a list index: when the position of one item is changed, the
positions of other items in the collection are updated in response.
PositionField to your model; that's just about it.
If you want to work with all instances of the model as a single collection,
there's nothing else required. To create collections based on one or more
fields on the model, specify the field names using the
The apps in
positions.examples demonstrate the
In general, the value assigned to a
PositionField will be handled like a
list index, to include negative values. Setting the position to
cause the item to be moved to the second position from the end of collection --
unless, of course, the collection has fewer than two elements.
Behavior varies from standard list indices when values greater than or less than
the maximum or minimum positions are used. In those cases, the value is handled
as being the same as the maximum or minimum position, respectively.
also a special case that will cause an item to be moved to the last position in
The PositionManager custom manager uses PositionQuerySet to provide a reposition method that will update the position of all objects in the queryset to match the current ordering. If reposition is called on the manager itself, all objects will be repositioned according to the default model ordering.
Be aware that, unlike repositioning objects one at a time using list indices, the reposition method will call the save method of every model instance in the queryset.
ManyToManyField as a
collection won't work; use an
intermediate model with a
class Product(models.Model): name = models.CharField(max_length=50) class Category(models.Model): name = models.CharField(max_length=50) products = models.ManyToManyField(Product, through='ProductCategory', related_name='categories') class ProductCategory(models.Model): product = models.ForeignKey(Product) category = models.ForeignKey(Category) position = PositionField(collection='category') class Meta(object): unique_together = ('product', 'category')
- Unique constraints can't be applied to
PositionFieldbecause they break the ability to update other items in a collection all at once. This one was a bit painful, because setting the constraint is probably the right thing to do from a database consistency perspective, but the overhead in additional queries was too much to bear.
- After a position has been updated, other members of the collection are updated
using a single SQL
UPDATEstatement, this means the
savemethod of the other instances won't be called. As a partial work-around to this issue, any
auto_now=Truewill be assigned the current time.