GithubHelp home page GithubHelp logo

jp74 / django-suit-sortable Goto Github PK

View Code? Open in Web Editor NEW
7.0 7.0 8.0 183 KB

Drag-and-drop ordering for objects and inlines in Django admin using django-suit.

License: BSD 3-Clause "New" or "Revised" License

Python 53.67% Shell 33.55% CSS 0.01% JavaScript 12.78%

django-suit-sortable's Introduction

django-suit-sortable

Drag-and-drop ordering for objects and inlines in Django admin using django-suit.

Installation

pip install -e git://github.com/JP74/django-suit-sortable.git#egg=suit_sortable

Configuration

  1. Add suit_sortable to your INSTALLED_APPS.
  2. Ensure django.core.context_processors.static is in your TEMPLATE_CONTEXT_PROCESSORS.

To use suit_sortable you must do following:

In your models.py file add integer property for sortable to you model:

from django.db import models

class Category(models.Model):
    ...
    position = models.PositiveIntegerField()

Adding Sortable to an existing model

If you're adding Sorting to an existing model, it is recommended that you use django-south to create a schema migration to add the "position" field to your model. You will also need to create a data migration in order to add the appropriate values for the position column.

Example assuming a model named "Category":

def forwards(self, orm):
    for index, category in enumerate(orm.Category.objects.all()):
        category.position = index + 1
        category.save()

Django Admin Integration

To enable sorting in the admin, you need to inherit from SortableAdmin and add a list_editable column for the position field:

from django.contrib import admin
from myapp.models import MySortableClass
from suit_sortable.admin import SortableAdmin

class MySortableAdminClass(SortableAdmin):
    list_display = (..., 'position',)
    list_editable = ('position',)
    """Any other admin options you need go here"""

admin.site.register(MySortableClass, MySortableAdminClass)

To enable sorting on TabularInline models, you need to inherit from SortableTabularInline:

from suit_sortable.admin import SortableTabularInline

class MySortableTabularInline(SortableTabularInline):
   """Your inline options go here"""

To enable sorting on StackedInline models, you need to inherit from SortableStackedInline:

from suit_sortable.admin import SortableStackedInline

class MySortableStackedInline(SortableStackedInline):
   """Your inline options go here"""

Limitations

Since sortables are based on JavaScript solution, there are known limitations:

  1. It doesn't work with pagination.

django-suit-sortable's People

Contributors

mishbahr avatar philippbosch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-suit-sortable's Issues

All inlines become sortable

I have multiple inlines a change form.
However, the javascript considers all inlines should be sortable, wether or not it's desired.
cf.

$('.inline-related')... // takes all inlines

This has bad side effects.
In my case, if my non-sortable inline has 0 items, it would break the rightful sortable inline because of this :

if($('.inline-related input[name$=-INITIAL_FORMS]').val() <= 1){
	return;
}

I will send a fix & PR for that.

null value in column "order" violates not-null constraint

I am using SortableTabularInline and cannot insert new inline items. The documentation covers SortableAdmin with an example, but it's not very specific about how to use django-suit-sortable for SortableTabularInline, so maybe i got something wrong.

this is my admin configuration:

class TipInline(SortableTabularInline):
    model = Tip
    extra = 0
    sortable = 'order'
    fields = ('username', 'text')
    suit_classes = 'suit-tab suit-tab-items'

When I create a new inline Tip, the order field is left null. Any idea how it can work?

Sortable won't work with tabular inline

Hi,
I tried django-suit-sortable,
I have encountered a problem where the drag and drop wont work (won't init, i guess) with my tabular inline.
I will describe the situation:
I have an item model which has several pictures held by Filer. I have an intermediate table to hold the image and link it to the item (to link numeral pictures to a single item).
So, I have:
Item model
Item2Picture model

Item2PictureInline

ItemAdmin calling the Item2PictureInline

As you suggested, I have a position field in Item2Picture model, and Item2PictureInline inherited from SortableTabularInline.
This work work, I do not know why.

However, I tried to add SortableAdmin to the ItemAdmin just to see if it will work, and it had. I was able to change the order of my items in the change list.

What seems to be the problem here, i ask? :(
Thanks in advance.

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.