Coder Social home page Coder Social logo

django-treewidget's Introduction

treewidget for Django

Provides the model fields TreeForeignKey, TreeOneToOneField, TreeManyToManyField for tree models with a tree widget for django. Uses jstree (thanks to vakata).

Tested with django-mptt and django-treebeard with Django 1.11 & 2.0.2. It will not work with Django <1.11.

Installation

  • pip install django-treewidget
  • place 'treewidget' in INSTALLED_APPS
  • for AJAX tree updates add the routes to your urls.py, e.g. url(r'^treewidget/', include('treewidget.urls'))

Usage

Just replace any foreign key, m2m or one2one tree model field with the provided counterpart. jstree depends on jQuery to work. This module does not provide a jQuery version, use the admin version or place your own version along with your other assets.

Workarounds

If used in admin along with collapsed fieldsets, django's jQuery gets loaded to late and jstree will fail to load. You can work around this issue by loading a separate jQuery with extend = False at your admin class, example:

class ExampleAdmin(admin.ModelAdmin):
    class Media:
        extend = False
        js = (
            'https://code.jquery.com/jquery-3.3.1.min.js',
        )

Customization

The fields understand two additional arguments:

  • settings: Dictionary containing the optional boolean values for 'show_buttons' (shows "Expand", "Collapse" and "Selected" buttons), 'search' (for in-tree search), 'dnd' (drag and drop support) and 'sort' (apply tree order in frontend). Defaults to {}.
  • treeoptions: Settings directly applied to jstree. Must be a JSON string, if given as argument to a field, otherwise a python dictionary. Defaults to treewidget.fields.TREEOPTIONS. Note that some widget settings will override treeoptions to keep working.

Both settings can be provided project wide in settings.py as TREEWIDGET_SETTINGS and TREEWIDGET_TREEOPTIONS.

It is possible to render a deeper nested branch (subtree) by overriding the default formatter. Just set the parent id to '#' for the branch's top level entries in the formatter's render method.

NOTE: If you use a prefiltered queryset which data does not form a subtree containing all parents up to the top level, jstree cannot render it correctly. With 'filtered' in settings set to True those querysets will be rendered by adding missing nodes as not selectable.

Example

from django.db import models
from mptt.models import MPTTModel
from treewidget.fields import TreeForeignKey

class Mptt(MPTTModel):
    name = models.CharField(max_length=32)
    parent = TreeForeignKey('self', blank=True, null=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

Renders like this:

screenshot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.