Login

Tag "tree"

Snippet List

jstree integration to django admin

You have some tree-like structure in your models, for example: `class MyModel(models.Model): parent = models.ForeignKey('self', verbose_name=u'Parent', \ null=True, blank=True, related_name='children') name = models.CharField(u'Раздел', max_lengtch=255) position = PositiveSmallIntegerField(u'Позиция', default=0) class Meta: ordering = ('position',)` To see it as a tree in admin's object list(you also can sort items, move to another parents by drag-n-drop; and rename them) add this to admin.py: `class MyModelAdmin(admin.ModelAdmin): ordering = ('position',) list_display = ('pk','name','parent','position') raw_id_fields =('parent',) list_per_page = 900 #we sould have all objects on one page list_editable = ('name','position','parent') def parent_id(self,obj): return obj.parent and obj.parent.id or '0' class Meta: model = MyModel class Media: js = [settings.MEDIA_URL + s for s in ('lib/jquery-1.3.2.min.js', 'lib/jquery.tree.min.js', 'lib/plugins/jquery.tree.contextmenu.js', 'lib/mymodel_admin.js',)] css = { 'all':(settings.MEDIA_URL+'css/nestedsortablewidget.css',) }` mymodel_admin.js is the code listed here, if you have different title field(not "name"), change var title_column in javascript, list_display and list_editable. jstree can be obtained here: [jstree](http://www.jstree.com/) screenshot is in [my blog](http://tabed.org/blog/2010/01/06/jstree-in-django-admin/)

  • admin
  • model
  • tree
Read More

outliner template tag

Let's say you have a dataset and you want to render a page with sections/subsections/subsubsections down to some arbitrary depth and with arbitrary keys. For example, you might want to show cars broken out by year/price_range or price_range/year or price_range/manufacturer/model. The outliner template tag allows you to support multiple breakdowns of your data in a DRY sort of way. In order to use it, you supply four things: 1. a template for surrounding each subsection (think turtles all the way down) 2. a queryset (really anything iterable) 3. sectionizer dictionary/objects (see sample code, you must supply key_method) 4. inner html to render the lowest subsections The template provides the following: 1. It recursively uses each of your sectionizers' key methods to divvy up data sets. 2. It surrounds each section with templates of your choosing. 3. It renders the inner template for all the "leaf" elements of your tree. 4. It supplies some handy context vars for things like depth and outline ids. What this is not: 1. this is not for arbitrary tree data--think of the tree as fixed depth 2. this is not as simple as the regroup tag--if you have a concrete organization to your data, you should keep it simple and hand-code your templates

  • template
  • tree
  • outline
  • recursive
Read More

Nested set abstraction for hierarchical data

The nested set abstraction is a very nice way to store hierarchical data, for example places, in a database. It provides an easy way to say that Melbourne is within Victoria, which is within Australia, etc.</p> The calls to addChild() are expensive, so this model is slow to build, and bad if you do frequent updates. If you're building a read-only model though, it's much faster than ForeignKey('self') for determining ancestor/descendent relationships.

  • tree
  • hierarchy
  • nested-set
Read More

4 snippets posted so far.