Code Monkey home page Code Monkey logo

django-sort-order-field's Introduction

Sort Order Field

Sort Order Field provides a Django model field for controlling sort order. The field includes an admin widget which makes it easy to arrange related models in an inline form.

At the moment, the admin widget assumes it is the only field which controls ordering for its model, and it has no awareness of the sort direction. This is less powerful than other sort field libraries, but its design may be useful for certain situations.

screencast showing sort buttons in action

Easy to use sort buttons.

screencast showing sort buttons in action

Accessible keyboard navigation and non-sequential sort indexes.

Installation

Add "sort_order_field" to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'sort_order_field',
]

Basic usage

Add a SortOrderField to one of your models:

from django.db import models
from sort_order_field import SortOrderField

class Poll(models.Model):
    question = models.CharField(max_length=100)

class PollOption(models.Model):
    class Meta:
        ordering = ('sort_order',)

    poll = models.ForeignKey('Poll', on_delete=models.CASCADE)
    sort_order = SortOrderField(_("Sort"))
    label = models.CharField(max_length=100)

Add the sortable model to an inline model admin for your parent model:

from django.contrib import admin
from . import models

class PollOptionInline(admin.TabularInline):
    model = models.PollOption
    fields = ('sort_order', 'label',)

@admin.register(models.Poll)
class PollAdmin(admin.ModelAdmin):
    fields = ('question',)
    inlines = (PollOptionInline,)

The sort order field will be rendered as a text field with JavaScript enhancements. When the user enters a new sort order value, the inline forms will be reorganized according to that value. The sort order fields will be bound by their initial values, which helps to avoid conflicts if the same objects appear in different inlines.

django-sort-order-field's People

Contributors

dylanmccall avatar dreizehnelf avatar

Watchers

James Cloos 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.