Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin tree management #57

Closed
django-mptt opened this issue Dec 19, 2010 · 20 comments
Closed

Admin tree management #57

django-mptt opened this issue Dec 19, 2010 · 20 comments

Comments

@django-mptt
Copy link
Collaborator

Originally GC 33

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.

@django-mptt
Copy link
Collaborator Author

insin (12 Oct 08):

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.

@django-mptt
Copy link
Collaborator Author

absolute.search (03 Feb 09):

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?

@django-mptt
Copy link
Collaborator Author

absolute.search (03 Feb 09):

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.

@django-mptt
Copy link
Collaborator Author

matthiask (26 Feb 09):

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)

@django-mptt
Copy link
Collaborator Author

matthiask (26 Feb 09):

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

@django-mptt
Copy link
Collaborator Author

T.Karbownicki (21 May 09):

Thanks for mptt_admin.diff! Works great!

@django-mptt
Copy link
Collaborator Author

o...@gezgin.com (29 Jun 09):

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.

@django-mptt
Copy link
Collaborator Author

o...@gezgin.com (30 Jun 09):

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.

@django-mptt
Copy link
Collaborator Author

andrepleblanc (14 Jul 09):

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?

@django-mptt
Copy link
Collaborator Author

o...@gezgin.com (15 Jul 09):

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(....).

@django-mptt
Copy link
Collaborator Author

michele.pasin (06 Aug 09):

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!

@django-mptt
Copy link
Collaborator Author

michele.pasin (12 Aug 09):

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?

@django-mptt
Copy link
Collaborator Author

matthiask (12 Aug 09):

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)

@django-mptt
Copy link
Collaborator Author

michele.pasin (18 Aug 09):

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!!!

@django-mptt
Copy link
Collaborator Author

d...@pdll.pl (19 Jan 10):

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.

@django-mptt
Copy link
Collaborator Author

a.schmid00 (26 Jan 10):

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?

@django-mptt
Copy link
Collaborator Author

T.Karbownicki (27 Apr 10):

I removed the bug that didn't increase tree_id.

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

@django-mptt
Copy link
Collaborator Author

michele.pasin (17 Jun 10):

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

@django-mptt
Copy link
Collaborator Author

craigds (28 Sep 10):

See google group discussion re admin changes for 0.4:

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

@django-mptt
Copy link
Collaborator Author

craigds (01 Oct 10):

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

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants