Export to GitHub

django-mptt - issue #33

Admin tree management


Posted on Sep 10, 2008 by Happy Lion

Attached is a patch that will allow a tree to be managed in the admin interface using the jQuery NestedSortableWidget (http://code.google.com/p/nestedsortables/wiki/NestedSortableWidgetDocumentation). I have shamelessly lifted some code from the Tusk CMS, credit goes to them for the idea (http://code.google.com/p/tusk-cms/). It's a little messy and hacky at the moment, but it should work.

Some sample usage in admin.py:

from django.contrib import admin from mptt.admin import MpttModelAdmin

class CategoryAdmin(MpttModelAdmin): list_display = ('title',)

admin.site.register(Category, CategoryAdmin)

The files in mptt_media.tar.gz need to be put in your MEDIA_URL directory.

Attachments

Comment #1

Posted on Oct 12, 2008 by Quick Camel

Thanks for this - it might be bit messy, but it's a great start in terms of how to get MPTT hooked into the admin. Haven't had time to look into it myself yet, so this will be a great help.

Comment #2

Posted on Feb 3, 2009 by Massive Dog

This is an excellent addition to an already already project, many thanks.

Although this isn't a django-mptt problem, per se: I have the patch working using Firefox on my mptt model but using IE7 I get a "Done, but with errors on page" message in the status area and no NestedSortableWidget is displayed. No further errors are displayed.

Is this a known problem?

Comment #3

Posted on Feb 3, 2009 by Massive Dog

I have solved the IE error problem by removing the spurious comma from the buildNSW () code in change_list.html:

... onSave: function(){ $('#my_widget').NestedSortableWidgetDestroy(); buildNSW(); }, <-- here ...

Hope that's of help to someone else.

Comment #4

Posted on Feb 26, 2009 by Happy Horse

Disclaimer: I'm the primary author of tusk cms, and I can't say I'm very proud of the code there.

In the meantime, we have completely rewritten the code and made it much more extensible. In fact, the tree view is already working for several models. There are still some things left such as respecting list_display. I don't think it makes much sense trying to add support for pagination or list_filter, but maybe someone else has a different opinion (and code to support it)

Comment #5

Posted on Feb 26, 2009 by Happy Horse

Oops. I wanted to add a link to an email I sent to django-users here, sorry for clicking submit too fast.

http://groups.google.com/group/django-users/browse_thread/thread/b543144640ac1d4a

Comment #6

Posted on May 21, 2009 by Swift Hippo

Thanks for mptt_admin.diff! Works great!

Comment #7

Posted on Jun 20, 2009 by Swift Kangaroo

Comment deleted

Comment #8

Posted on Jun 29, 2009 by Quick Panda

The possibility of using MPTT in admin made me start using it. Thanks for your efforts.

However, the data does not show up in the change list view, and the url to sen the json object (http://localhost:8000/admin/bio/taxonomy/json/?rnd=...) raises an exception:

"invalid literal for int() with base 10: 'json'"

I am using a recent SVN version of Django. I think in the MpttModelAdmin, get_urls() method should be overriden instead of the deprecated call(). But I couldn't figure out how.

I tried copying the get_urls() into the MpttModelAdmin and adding the json url as such:

def get_urls(self):
    [...]

    urlpatterns = patterns('',
        [...]
        url(r'^(.+)/$',
            wrap(self.change_view),
            name='%sadmin_%s_%s_change' % info),
        url(r'^(.+)/json/$',
            wrap(self.json_view),
            name='%sadmin_%s_%s_json' % info),
    )
    return urlpatterns

but i am still getting the same error.

Comment #9

Posted on Jun 30, 2009 by Quick Panda

I should be more careful with the urlpatterns. I get it working like this:

def get_urls(self): ... urlpatterns = patterns('', url(r'^json/$', wrap(self.json_view), name='%sadmin_%s_%s_json' % info), ...

But _build_tree() method also needs to be modified to supply the extra 'form' parameter required by items_for_result() function:

def _build_tree(nodes, cl): ret = [] for obj in nodes: dic = { 'id': obj.id, 'info': list(items_for_result(cl, obj, form=None)), # form param added }

Though the resulting form does look a bit deformed because of the checkboxes added, it works.

Comment #10

Posted on Jul 14, 2009 by Grumpy Ox

I tried implementing your fixed with the overridden get_urls but I can't figure out where your 'info' variable is coming from, what is it?

Comment #11

Posted on Jul 15, 2009 by Quick Panda

Sorry, I've cropped that line too when pasting. 'info' is:

info = self.admin_site.name, self.model._meta.app_label, self.model._meta.module_name

Set it just before urlpatterns = patterns(....).

Comment #12

Posted on Aug 6, 2009 by Helpful Camel

the original diff posted at the top of this page worked all right for me !! Thanks!!! (I'm running Django 1-0 )

Only thing I had to fix was the path to the media files in new admin.py file, e.g.: instead of "mptt/jquery-1.2.6.min.js" I put something like "/pathtomymedia/mptt/jquery-1.2.6.min.js"... I guess that an alternative solution is to modify the urls.py file accordingly!

Attachments

Comment #13

Posted on Aug 12, 2009 by Helpful Camel

Hi there - a small update: the tree visualization needs to be improved cause if there are a lot of lines (e.g. more than 1000) the js runs into memory problems and it crashes. I guess that one solution would be a pagination mechanism; another one is building a mechanism to open/close the nodes and load the children-data on demand, via ajax. Unfortunately I don't have much time to implement a fix now, but if I do I'll be posting it here... Has any of you run into similar problems?

Comment #14

Posted on Aug 12, 2009 by Happy Horse

Disclaimer: I'm the original author of tusk CMS, which I've abandoned a long time ago. I'd recommend anyone interested in managing tree stuctures to look into FeinCMS, which has a ModelAdmin subclass that you can just inherit from to get a tree administration interface; it's called feincms.admin.tree_editor.TreeEditor. More information here:

http://spinlock.ch/pub/feincms/ (Screenshots & Links)

Comment #15

Posted on Aug 18, 2009 by Helpful Camel

Hi there - to whoever might be interested - I posted a bunch of simple instructions for using MPTT with FeinCms adminTree class here:

http://magicrebirth.wordpress.com/2009/08/18/django-admin-and-mptt-2/

Matthias, tx again for the useful tips!!!

Comment #16

Posted on Jan 19, 2010 by Massive Cat

I found a bug in admin json.js: when we drag element from level1 (tree_id=1) to level0 and save, the tree_id will be still 1. But when we add a new element to level0 he will have tree_id=2. This bug of holding the tree_id number may cause errors when you use get_root() - there will be two elements with 'parent__isnull': True and 'tree_id': 1.

Comment #17

Posted on Jan 26, 2010 by Happy Dog

im on django 1.1.1 using sqlite3 and if i want to look at my objects in the admin i get a "Could not load the data from the server." any hints?

Comment #18

Posted on Apr 27, 2010 by Swift Hippo

I removed the bug that didn't increase tree_id.

admin.py -if parent == 'NULL': +if parent == None:

Attachments

Comment #19

Posted on Jun 17, 2010 by Helpful Camel

People - with relation to my comment above [n. 15] - the example code have moved here: http://www.michelepasin.org/techblog/?p=275 Thanks, m

Comment #20

Posted on Sep 3, 2010 by Helpful Monkey

(No comment was entered for this change.)

Comment #21

Posted on Sep 6, 2010 by Helpful Monkey

(No comment was entered for this change.)

Comment #22

Posted on Sep 28, 2010 by Helpful Monkey

See google group discussion re admin changes for 0.4:

http://groups.google.com/group/django-mptt-dev/browse_thread/thread/63f9fc221246f527

Comment #23

Posted on Oct 1, 2010 by Helpful Monkey

Done in 0.4, with a bare-bones ModelAdmin and a FeinCMS one for those with FeinCMS

Status: Fixed

Labels:
Type-Enhancement Priority-Medium Milestone-Release0.4