Issue 33: Admin tree management
Status:  Fixed
Owner:
Closed:  Sep 2010
Reported by ben.firs...@gmail.com, Sep 10, 2008
Attached is a patch that will allow a tree to be managed in the admin
interface using the jQuery NestedSortableWidget
(https://code.google.com/p/nestedsortables/wiki/NestedSortableWidgetDocumentation).
I have shamelessly lifted some code from the Tusk CMS, credit goes to them
for the idea (https://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.
mptt_admin.diff
7.6 KB   View   Download
mptt_media.tar.gz
59.7 KB   Download
Oct 12, 2008
Project Member #1 jonathan.buchanan
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.
Status: Chatting
Owner: jonathan.buchanan
Labels: -Type-Defect Type-Enhancement
Feb 2, 2009
#2 absolute...@googlemail.com
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?


Feb 2, 2009
#3 absolute...@googlemail.com
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.

Feb 26, 2009
#4 matthias...@gmail.com
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)
Feb 26, 2009
#5 matthias...@gmail.com
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
May 21, 2009
#6 T.Karbow...@gmail.com
Thanks for mptt_admin.diff! Works great!
Jun 29, 2009
#8 omat%gez...@gtempaccount.com
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.

Jun 29, 2009
#9 omat%gez...@gtempaccount.com
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. 

Jul 14, 2009
#10 andreple...@gmail.com
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?

Jul 14, 2009
#11 omat%gez...@gtempaccount.com
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(....).
Aug 6, 2009
#12 michele.pasin
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! 
Picture 2.png
33.7 KB   View   Download
Aug 12, 2009
#13 michele.pasin
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? 
Aug 12, 2009
#14 matthias...@gmail.com
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)
Aug 18, 2009
#15 michele.pasin
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!!!
Jan 19, 2010
#16 adwo...@pdll.pl
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.
Jan 26, 2010
#17 a.schmi...@gmail.com
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?

Apr 27, 2010
#18 T.Karbow...@gmail.com
I removed the bug that didn't increase tree_id.

admin.py
-if parent == 'NULL':
+if parent == None:
mptt_admin.diff
7.6 KB   View   Download
Jun 17, 2010
#19 michele.pasin
People - with relation to my comment above [n. 15] - the example code have moved here:
http://www.michelepasin.org/techblog/?p=275
Thanks, 
m
Sep 3, 2010
Project Member #20 craig.ds@gmail.com
(No comment was entered for this change.)
Status: Accepted
Owner: craig.ds
Sep 6, 2010
Project Member #21 craig.ds@gmail.com
(No comment was entered for this change.)
Labels: Milestone-Release0.4
Sep 27, 2010
Project Member #22 craig.ds@gmail.com
See google group discussion re admin changes for 0.4:

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

Status: Chatting
Sep 30, 2010
Project Member #23 craig.ds@gmail.com
Done in 0.4, with a bare-bones ModelAdmin and a FeinCMS one for those with FeinCMS
Status: Fixed