jrief / django-admin-sortable2 Goto Github PK
View Code? Open in Web Editor NEWGeneric drag-and-drop ordering for objects in the Django admin interface
Home Page: https://django-admin-sortable2.readthedocs.io/en/latest/
License: MIT License
Generic drag-and-drop ordering for objects in the Django admin interface
Home Page: https://django-admin-sortable2.readthedocs.io/en/latest/
License: MIT License
in class SortableAdminMixin
def save_model(self, request, obj, form, change):
if not change:
setattr(obj, self.default_order_field, self.get_max_order() + 1)
super(SortableAdminMixin, self).save_model(request, obj, form, change)
that maybe can fix
Only to say that to make working django-admin-sortable2 with Django CMS 3.0.0.beta3 (pip install https://github.com/divio/django-cms/archive/3.0.0.beta3.zip) I needed to modify django-admin-sortable2 / adminsortable / admin.py the following way
class SortableAdminBase(object):
class Media:
css = { 'all': ('adminsortable/css/sortable.css',) }
if VERSION[0] == 1 and VERSION[1] <= 5:
js = (
'adminsortable/js/plugins/admincompat.js',
'adminsortable/js/libs/jquery.ui.core-1.7.1.js',
'adminsortable/js/libs/jquery.ui.sortable-1.7.1.js',
)
else:
js = (
'adminsortable/js/plugins/admincompat.js',
'adminsortable/js/libs/jquery.ui.core-1.10.3.js',
'adminsortable/js/libs/jquery.ui.widget-1.10.3.js',
'adminsortable/js/libs/jquery.ui.mouse-1.10.3.js',
'adminsortable/js/libs/jquery.ui.sortable-1.10.3.js',
)
if 'cms' in settings.INSTALLED_APPS:
from cms import version
if version[0] < 3:
js = (
'cms/js/plugins/admincompat.js',
'cms/js/libs/jquery.query.js',
'cms/js/libs/jquery.ui.core.js',
'cms/js/libs/jquery.ui.sortable.js',
)
Kr,
Patrick
enable_sorting
is set in get_changelist
and checked during gathering of media
.
However, on the detail page get_changelist
is not called and therefore accessing enable_sorting
results in an AttributeError
that is silenced by the media machinery (afaik). This somehow leads to bad media order (Django's jQuery not loaded on top, but in the middle of the stack). This results in JavaScript errors on the page.
A workaround is settings enable_sorting = False
on your ModelAdmin
subclass. But this should probably be set on the Mixin itself.
Any time the original value of a model instance (as determined by its str method) contains a character upon which word wrap could occur, such as space or dash, the rest of the value is word wrapped out of visibility.
As an example, create a TabularInline for the following model:
from localflavor.us.models import PhoneNumberField
@python_2_unicode_compatible
class PhoneNumber(models.Model):
other_model = models.ForeignKey('OtherModel')
number = PhoneNumberField('phone number')
order = models.PositiveSmallIntegerField(default=0)
class Meta(object):
ordering = ('order',)
def __str__(self):
return self.number
After saving the phone number '123-456-7890', the original value will be subsequently displayed as '123-'.
I recently updated my installation of django-admin-sortable2 using "pip install --upgrade django-admin-sortable2", and it did something I didn't expect and absolutely don't want to happen: pip also updated my Django (to 1.6.2). It appears to have done this because of the install_requires=['Django>=1.4']
parameter to setup()
.
This is extremely dangerous, because as I understand it, pip will gladly upgrade Django to 1.7 (whenever that comes out officially on PyPI). This would be disastrous! Even upgrades to minor point releases would be bad, because the user didn't ask for them.
I've looked at a few other popular Django apps, and none of them use an install_requires
setting in their setup.py files. So I think the best course of action is to remove it.
There is a small mistake on file inline-sortable.js, line 5. You used the $=
selector to find and fill the sort field, which is ambiguous because it automatically fills the sort number to all fields that end with the value set at default_order_field
. For instance, I have a table with two sort fields: sort_order
and filter_sort_order
, and I only want it to sort by sort_order
.
@teddybeermaniac Today I came across a nasty regression introduced by #74.
Whenever list-sortable.js is included by a detail view, the change_list.html template can't initialize the global variables sortable_current_page and sortable_total_pages. Then accessing these variables results into a Undefined exception.
Patch for this is on the way. Please recheck in your envionment.
"Order" field don't hide. For now I use a workaround to solve it. In template tabular-1.5.html I changed this:
<td{% if field.field.name %} class="field-{{ field.field.name }}{% if field.field.is_hidden %} is_hidden{% endif %}"{% endif %}>
to this:
<td{% if field.field.name %} class="field-{{ field.field.name }}{% if field.field.name == inline_admin_formset.formset.default_order_field %} is_hidden{% endif %}"{% endif %}>
It would be very handy to have a Changelog documenting the various releases in the future.
When using the SortableAdminMixin I keep getting 'object has no attribute 'get_queryset''.
# admin.py
from adminsortable2.admin import SortableAdminMixin,
...
class ListAdmin(SortableAdminMixin, admin.ModelAdmin):
list_per_page = 20
list_display = ('title', 'created_by', 'publish_date', 'expire_date',
'status', 'comments', 'is_featured', 'bookmatch')
search_fields = ['title', ]
filter_horizontal = ('tags',)
list_filter = ['status', ListDeleteRequestFilter,
IsFeaturedFilter, 'bookmatch', 'tags', ]
inlines = (ListItemInlineAdmin,)
actions = [draft_list, inreview_list, publish_list, archive_list,
feature_list, unfeature_list]
admin.site.register(List, ListAdmin)
# part of the traceback
...
File ".../lib/python2.7/site-packages/adminsortable2/admin.py" in get_actions
86. paginator = self.get_paginator(request, self.get_queryset(request), self.list_per_page)
Exception Type: AttributeError at /admin/selects/list/
Exception Value: 'ListAdmin' object has no attribute 'get_queryset'
Am I doing something wrong or is this a bug?
It would be useful if django-admin-sortable2 would ship with embedded translations as the code is correctly setup. I can help if needed
Just need some confirmation on whether this is a bug. On tabular inlines, instead of just the "sort" area activating the drag and drop, then entire row activates it. I can click anywhere in the tabular row and drag the row around.
From the docs it seems like the desired behavior is that only the spot to the very left in the 'sort' column should activate drag.
Looking to dive in and fix this, but not sure if it's intended behavior. Thanks!
When using a html minifier like django-htmlmin the reorder handle disappears because
is removed from drag div.
Without htmlmin:
<td class="field-_reorder"><div class="drag" order="1"> </div></td>
With htmlmin:
<td class="field-_reorder"><div class="drag" order="1"></div></td>
Had to exclude admin from the minifier:
EXCLUDE_FROM_MINIFYING = (r'^admin/', )
Just wanted to point this out.
Hey,
thanks for your nifty tool first.
So I'm using this application with django cms 3 and I have following problem with inline sortables:
When I try to click on a or tag, I automatically start dragging that object. As a workaround I can click with right click on that input field first and then it is selected and I can write in it or I can tab till to the input field. Strangely this problem is only in firefox. On a webkit based browser there is no problem.
I have django-admin-sortable2 running on Django 1.6 on a branch (https://github.com/julienp/django-admin-sortable2/tree/django16) but I am not sure what would be a good way to keep it compatible with previous versions.
The two issues I ran into were:
The first issue is easy enough to fix in a backwards compatible way (use transaction.atomic if it's available, with a fallback to commit_on_success).
The jQueryUI issue is trickier to fix. I suppose django-admin-sortable2 could ship its own jQuery version and always use that.
Do you have any plans regarding Django 1.6? Keeping the changes in a separate branch works fine for me now.
I have a number of projects where I currently use django-admin-sortable, but would like to use django-admin-sortable2 because of its clean and lightweight approach to solving the sortable FK problem.
Unfortunately, because both libraries use the same package name: adminsortable
, it is rather difficult if not impossible to use them side-by-side in the same project during transition. It also means that all of my migration history has to be either rewritten or discarded, because the existing migrations reference adminsortable.SortableForeignKey
, which does not exist in this package.
I propose (and will submit a PR for shortly) that we change the application package name to 'adminsortable2'. This will have only the smallest implications to new users, but means many more people will be able to use this library.
I think there is a bug.
{% url 'admin:sortable_update' %}
always return 'sortable_update' url for last registered url SortableAdmin.
For example, I have three models: ModelOne, ModelTwo, ModelThree.
All this model are registered as SortableAdmin instances for admin panel.
I go to /admin/example_app/model_three and see that
var sortable_current_page = '/admin/example_app/model_three/adminsortable2_update';
It's fine, but when I go to modelOne or modelTwo page, the link is the same. So if I drag some ModelOne/ModelTwo the order field will be update for ModelThree instances (or error if instance with same order field value is absent)
As you see url for sort_update is wrong (it's url for some Section model, but page is for Contacts list)
It's error for case when I try to sort some instance of MyModel with order=X but OtherModel.objects.get(order=X) is absent
When combinig SortableInlineAdminMixin and TabularInline classes, the column titles doesn't match the column contents any more.
I seems that the content in the column after the "Sort" column is not rendered properly into the view. It is shifted one column further. See screenshot.
Using:
Django==1.8.7
django-admin-sortable2==0.6.2
adminsortable2/admin.py", line 60, in init
self.exclude = [self.default_order_field] + self.exclude
TypeError: can only concatenate list (not "tuple") to list
Apparently, in Django 1.7, they've depreciated BaseModelAdmin.queryset in favor of get_queryset and are planning to remove in Django 1.8. Just wanted to give you a heads up since I'm getting the warnings.
It would be nice if django-admin-sortable2 checked for order_with_respect_to
. If it's enabled, repositioning queries could only update a subset of queryset (all records with the same value in order_with_respect_to
field as the one being repositioned). This should be pretty easy to implement so I might come up with a pull request.
I upgraded Sortable2 today and now my inline admin change form is not displaying properly.
I used collectstatic
.
I think I narrowed down the cause to the fact that <th{% if forloop.first %} colspan="2"{% endif %}
is not being applied to my first field from the tabular-1.5.html
template.
In a browser (Chrome; using Inspect Elements), if I add colspan="2"
to the first (technically second after "Sort") column it displays properly.
It is possible to make it compatible with django-modeltranslation for inlines (when is used TabbedTranslationAdmin - tabs isn't rendered for inlines)? It seems like there is some js incompatibility problem. I use django 1.5 and the latest versions of django-modeltranslation.
Thanks.
In a model admin, suppose we have two inline models: one is sortable and the other is not. If I add the SortableInlineAdminMixin to the sortable model, on the admin page, the entries of the other (unsortable) model become draggable as well (however, the drag-and-drop has no actual effects because there is no ordering).
I think the problem lies in the inline-sortable.js file: it simply adds drag-and-drop support to all inline models without checking they are sortable or not. And if there is one sortable inline model, this js will be included and thus all inline models are draggable.
Uncaught TypeError: jQuery.getQueryParams is not a function
It might be that jquery is not yet ready at the point where list-sortable functions are called.
I tried removing inlines, admin-style, but the issue stays. Any idea what this could be? I am running 0.6.3 and also had this issue with 0.6.2
Thanks for any help.
when actions = none the sorting bar gets really long and sorting only works once.
when you try to move an item a second time it uses the old id.
When using the SortableInlineAdminMixin class the new Django 1.8 feature InlineModelAdmin.show_change_link does't work any more.
To reproduce:
Using:
Django==1.8.7
django-admin-sortable2==0.6.2
Hello,
think i found a little bug in the management command reorder.
if i have a negative ordering ( ordering=('-position',) ) the reorder command fails silently
this could be fixed:
in management.command.reorder line 24
orderfield = Model._meta.ordering[0]
should be replaced with
orderfield = Model._meta.ordering[0]
if orderfield[0] == '-':
orderfield = orderfield[1:]
Executing a "reorder" on a table with hundred of thousands records require a lot of memory and time.
The problem come from the loop:
for obj in Model.objects.all():
which usually load in memory the whole resultset.
Try instead something like:
for obj in Model.objects.iterator():
When adding field entries to BaseModelAdmin.fields in one of the InlineModelAdmin classes django-admin-sortable throws a KeyError. I didn't test it with the ModelAdmin class.
This is probably due to field that holds the position value, which is marked as hidden. But this is not good, because sometimes you need to specify fields.
Also it would be good if you can display the position value in an inline. This makes sense when you have a lot of inlines and you want to place an inline on a certain position.
Using:
Django==1.8.7
django-admin-sortable2==0.6.2
When I try to install django-admin-sortable2==0.6.2 in Docker with pip3 this error occurs.
Error:
Collecting django-admin-sortable2==0.6.2 (from -r /tmp/requirements.txt (line 11))
Downloading django-admin-sortable2-0.6.2.tar.gz (47kB)
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-7uiq52zk/django-admin-sortable2/setup.py", line 33, in <module>
long_description=convert('README.md', 'rst'),
File "/tmp/pip-build-7uiq52zk/django-admin-sortable2/setup.py", line 11, in convert
return fd.read()
File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 876: ordinal not in range(128)
The Fix works with codecs instead of io
Fix:
try:
from pypandoc import convert
except ImportError:
import codecs
def convert(filename, fmt):
with codecs.open(filename, 'r', 'utf-8') as fd:
return fd.read()
By default, TabularLine show more line than necessary in the admin, to simplify the adding process.
I've a bug with django-admin-sortable2 and that feature. Django-admin-sortable2 keep trying to reorder un existing fields, leading me to a field required error when I try to save.
If I remove the SortableInlineAdminMixin option, everything work, even manual reordering.
It look like django-admin-sortable2 try to edit the order settings for each field, even the one without values.
I love this plugin (app? I'm new to django, so I don't know the right terminology)! It made it super easy to add drag-and-drop sorting to my existing project, which I was expecting to be a major nightmare.
My only problem is that as soon as I had my tabular inline sorting up and running, there was suddenly an extra <td>
in my table, with class "original", which was pushing the <td>
s with my input fields out of the way, and making everything look ugly.
I sort of see what it might be useful for, but the part of the template that adds it seems like it might be leftover code from your debugging phase or something. I say this because there's no matching <th>
for it, which is what really threw me for a loop when I first saw it. I assumed that the template had somehow mis-ordered my input fields, when in fact they had all simply been pushed one column to the right by the "original" <td>
.
So, whatever this "original" <td>
is supposed to be for, I think it should either be removed or made optional with some kind of configuration setting (and defaulted to "off").
I'm guessing it's because my order field isn't called 'order' and this is hardcoded:
_reorder.admin_order_field = 'order'
I want new items to appear up top, so I reverted the sort order on the model:
class Meta:
ordering = ('-order',)
The order shown in the admin is correct, but all strange things happen when I sort. I can never get an item in the right place, they jump back or end up higher or lower.
I'm getting this JS error when I'm viewing a specific Artwork Category and viewing the tabular inline of all the artwork.
Here is my setup:
class ArtworkAdminInline(SortableInlineAdminMixin, admin.TabularInline):
model = Artwork
image_thumb = image_file('obj.image', short_description='Thumbnail')
readonly_fields = ('image_thumb',)
class ArtworkCategoryAdmin(SortableAdminMixin, admin.ModelAdmin):
inlines = [ArtworkAdminInline]
I'm not sure why it's including list-sortable.js
on this page since all it needs is just inline-sortable.js
. If I restart my localhost server this will work fine and only include inline-sortable
. But if I browse back to the list of Artwork Categories and then go into a different Artwork Category, then it includes both of those js files and I get the error.
I'm using Django 1.8 and django-admin-sortable2 0.6.0
when adding a new inline, it appears at first position after save, although when adding, it as added at last position. one must not order before save (common use case...) to reproduce this.
when addding default=999
in the order field definition, it works as expected. I'm not sure how it should behave, as the docs say default=0
...?
I have a parent abstract class:
class Media(models.Model):
...
order = models.PositiveSmallIntegerField("Order", blank=False, null=False )
class Meta:
abstract = True
and a child as:
class ModelImage(Media):
file = ImageField(upload_to=get_path)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
verbose_name = _('Image')
verbose_name_plural = _('Images')
ordering = ['order']
When changed the position in admin order field is not updating ? I am using django 1.6 with python 3.3... When not using abstract model everything works fine
django-admin-sortable2/adminsortable/admin.py", line 74, in init
AttributeError: class Media has no attribute 'js'
I find that SortableAdminMixin works well with django-parler. But SortableInlineAdminMixin isn't. So right now if a model is both translatable and inline sortable, I'm out of luck. Not sure how to achieve something like: class BlahAdmin(SortableInlineAdminMixin, TranslatableTabularInline).
Hello,
I agree with the philosophy behind this rewrite. One problem is that I am using https://github.com/sehmaschine/django-grappelli which seems to override important bits needed for the SortableInlineAdminMixin
.
Do you have a thought about adding some support or else advise on how to proceed?
- Kenn
The project documentation on readthedocs doesn't yet reflect the new package name in several places. Opening a bug for this since it's likely to trip up potential users trying to follow the docs. Here are the occurrences I found:
http://django-admin-sortable2.readthedocs.org/en/latest/installation.html#configuration
INSTALLED_APPS = (
...
'adminsortable',
...
)
http://django-admin-sortable2.readthedocs.org/en/latest/usage.html#integrate-into-a-list-view
from adminsortable.admin import SortableAdminMixin
http://django-admin-sortable2.readthedocs.org/en/latest/usage.html#integrate-into-a-detail-view
from adminsortable.admin import SortableInlineAdminMixin
Steps to reproduce:
Seen with 0.5.1, Django 1.6.11, Safari on OS X 10.10.4
Hello! I've been using django-admin-sortable2 to sort the order of pictures in my carousel image galleries. It worked marvelously. But lately,
I'm using Django==1.6.7
I write the following code to make a ListAdmin sortable:
from django.contrib import admin
from mall.models import HomepageItem
from adminsortable.admin import SortableAdminMixin
class HomepageItemAdmin(SortableAdminMixin, admin.ModelAdmin):
pass
admin.site.register(HomepageItem, HomepageItemAdmin)
Once I run the server and browse the root URL, I get the TypeError exception, which lies in: https://github.com/jrief/django-admin-sortable2/blob/master/adminsortable/admin.py#L129
The MethodType (imported from types) takes only two arguments, but there are three in the code.
Hi,
When I drag and drop the model datas from one row to another, they are not updated in the db. Thus when I refresh the page it's the same. What am I missing?
Hi, im using Django 1.7 and im geting this error when in run python manage.py runserver:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/home/matias/env/sudestada/local/lib/python2.7/site-packages/django/core/management/init.py", line 385, in execute_from_command_line
utility.execute()
File "/home/matias/env/sudestada/local/lib/python2.7/site-packages/django/core/management/init.py", line 354, in execute
django.setup()
File "/home/matias/env/sudestada/local/lib/python2.7/site-packages/django/init.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/matias/env/sudestada/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/matias/env/sudestada/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
ImportError: No module named adminsortable
When adding inline objects (SortableInlineAdminMixin) with the plus button, first they appear at the bottom of the list of inlines.
When saved they become the first item. To reproduce:
I was using Firefox 42.0. Django setup:
Django==1.8.6
django-admin-sortable2==0.6.0
Somewhat related to #76
if you define "negative" ordering (ordering = ('-position',)) the admin actions which moves the selected item(s) back don't work. The "endorder" value in _bulk_move gets a completely wrong value.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.