Git Product home page Git Product logo

django-positions's Introduction

Django Positions

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.

Usage

Add a 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 collection argument.

The apps in positions.examples demonstrate the PositionField API.

Indices

In general, the value assigned to a PositionField will be handled like a list index, to include negative values. Setting the position to -2 will 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. None is also a special case that will cause an item to be moved to the last position in its collection.

Bulk updates

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.

Many-to-many

Specifying a ManyToManyField as a collection won't work; use an intermediate model with a PositionField instead:

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')

Limitations

  • Unique constraints can't be applied to PositionField because 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 UPDATE statement, this means the save method of the other instances won't be called. As a partial work-around to this issue, any DateTimeField with auto_now=True will be assigned the current time.

django-positions's People

Contributors

bashmish avatar fabiocorneti avatar jezdez avatar jpwatts avatar mrbox avatar smulloni avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.