django-polymorphic-tree


Namedjango-polymorphic-tree JSON
Version 2.1 PyPI version JSON
download
home_pagehttps://github.com/django-polymorphic/django-polymorphic-tree
SummaryA polymorphic mptt structure to display content in a tree.
upload_time2021-11-18 10:28:45
maintainer
docs_urlNone
authorDiederik van der Boor
requires_python
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            django-polymorphic-tree
=======================

.. image:: https://github.com/django-polymorphic/django-polymorphic-tree/actions/workflows/tests.yaml/badge.svg?branch=master
    :target: https://github.com/django-polymorphic/django-polymorphic-tree/actions/workflows/tests.yaml
.. image:: https://img.shields.io/pypi/v/django-polymorphic-tree.svg
    :target: https://pypi.python.org/pypi/django-polymorphic-tree/
.. image:: https://img.shields.io/pypi/l/django-polymorphic-tree.svg
    :target: https://pypi.python.org/pypi/django-polymorphic-tree/
.. image:: https://img.shields.io/codecov/c/github/django-polymorphic/django-polymorphic-tree/master.svg
    :target: https://codecov.io/github/django-polymorphic/django-polymorphic-tree?branch=master


This package combines django-mptt_ with django-polymorphic_.
You can write Django models that form a tree structure where each node can be a different model type.

Example uses:

* Build a tree of organisation and company types (e.g. ``Partner``, ``Reseller``, ``Group`` and ``Customer``)
* Build a tree of a root node, category nodes, leaf nodes, each with custom fields.
* Build a todo list of projects, categories and items.
* Build a book of chapters, sections, and pages.

Origin
------

This code was created in django-fluent-pages_, and extracted to become a separate package.
This was done during contract work at Leukeleu_ (known for django-fiber_).


Installation
============

First install the module, preferably in a virtual environment::

    pip install django-polymorphic-tree

Or install the current repository::

    pip install -e git+https://github.com/django-polymorphic/django-polymorphic-tree.git#egg=django-polymorphic-tree

The main dependencies are django-mptt_ and django-polymorphic_,
which will be automatically installed.

Configuration
-------------

Next, create a project which uses the application::

    cd ..
    django-admin.py startproject demo

Add the following to ``settings.py``:

.. code:: python

    INSTALLED_APPS += (
        'polymorphic_tree',
        'polymorphic',
        'mptt',
    )


Usage
-----

The main feature of this module is creating a tree of custom node types.
It boils down to creating a application with 2 files:

The ``models.py`` file should define the custom node type, and any fields it has:

.. code:: python

    from django.db import models
    from django.utils.translation import gettext_lazy as _
    from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey


    # A base model for the tree:

    class BaseTreeNode(PolymorphicMPTTModel):
        parent = PolymorphicTreeForeignKey('self', blank=True, null=True, related_name='children', verbose_name=_('parent'))
        title = models.CharField(_("Title"), max_length=200)

        class Meta(PolymorphicMPTTModel.Meta):
            verbose_name = _("Tree node")
            verbose_name_plural = _("Tree nodes")


    # Create 3 derived models for the tree nodes:

    class CategoryNode(BaseTreeNode):
        opening_title = models.CharField(_("Opening title"), max_length=200)
        opening_image = models.ImageField(_("Opening image"), upload_to='images')

        class Meta:
            verbose_name = _("Category node")
            verbose_name_plural = _("Category nodes")


    class TextNode(BaseTreeNode):
        extra_text = models.TextField()

        # Extra settings:
        can_have_children = False

        class Meta:
            verbose_name = _("Text node")
            verbose_name_plural = _("Text nodes")


    class ImageNode(BaseTreeNode):
        image = models.ImageField(_("Image"), upload_to='images')

        class Meta:
            verbose_name = _("Image node")
            verbose_name_plural = _("Image nodes")


The ``admin.py`` file should define the admin, both for the child nodes and parent:

.. code:: python

    from django.contrib import admin
    from django.utils.translation import gettext_lazy as _
    from polymorphic_tree.admin import PolymorphicMPTTParentModelAdmin, PolymorphicMPTTChildModelAdmin
    from . import models


    # The common admin functionality for all derived models:

    class BaseChildAdmin(PolymorphicMPTTChildModelAdmin):
        GENERAL_FIELDSET = (None, {
            'fields': ('parent', 'title'),
        })

        base_model = models.BaseTreeNode
        base_fieldsets = (
            GENERAL_FIELDSET,
        )


    # Optionally some custom admin code

    class TextNodeAdmin(BaseChildAdmin):
        pass


    # Create the parent admin that combines it all:

    class TreeNodeParentAdmin(PolymorphicMPTTParentModelAdmin):
        base_model = models.BaseTreeNode
        child_models = (
            models.CategoryNode,
            models.TextNode,  # custom admin allows custom edit/delete view.
            models.ImageNode,
        )

        list_display = ('title', 'actions_column',)

        class Media:
            css = {
                'all': ('admin/treenode/admin.css',)
            }


    admin.site.register(models.CategoryNode, BaseChildAdmin)
    admin.site.register(models.TextNode, TextNodeAdmin)
    admin.site.register(models.ImageNode, BaseChildAdmin)
    admin.site.register(models.BaseTreeNode, TreeNodeParentAdmin)


The ``child_models`` attribute defines which admin interface is loaded for the *edit* and *delete* page.
The list view is still rendered by the parent admin.


Tests
-----

To run the included test suite, execute::

    ./runtests.py

To test support for multiple Python and Django versions, you need to follow steps below:

* install project requirements in virtual environment
* install python 2.7, 3.4, 3.5, 3.6 python versions through pyenv (See pyenv (Linux) or Homebrew (Mac OS X).)
* create .python-version file and add full list of installed versions with which project have to be tested, example::

    2.6.9
    2.7.13
    3.4.5
    3.5.2
    3.6.0
* run tox from the repository root::

    pip install tox
    tox

Python 2.7, 3.4, 3.5 and 3.6 and django 1.8, 1.10 and 1.11 are the currently supported versions.

Todo
----

* Sphinx Documentation


Contributing
------------

This module is designed to be generic. In case there is anything you didn't like about it,
or think it's not flexible enough, please let us know. We'd love to improve it!

If you have any other valuable contribution, suggestion or idea,
please let us know as well because we will look into it.
Pull requests are welcome too. :-)


.. _Leukeleu: http://www.leukeleu.nl/
.. _django-fiber: https://github.com/ridethepony/django-fiber
.. _django-fluent-pages: https://github.com/edoburu/django-fluent-pages
.. _django-mptt: https://github.com/django-mptt/django-mptt
.. _django-polymorphic: https://github.com/django-polymorphic/django-polymorphic




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/django-polymorphic/django-polymorphic-tree",
    "name": "django-polymorphic-tree",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Diederik van der Boor",
    "author_email": "opensource@edoburu.nl",
    "download_url": "https://files.pythonhosted.org/packages/bb/5d/bc85eb92c6f28aca811fa534d1f414495ed7bc6d37e6f3b6d2fa36849064/django-polymorphic-tree-2.1.tar.gz",
    "platform": "",
    "description": "django-polymorphic-tree\n=======================\n\n.. image:: https://github.com/django-polymorphic/django-polymorphic-tree/actions/workflows/tests.yaml/badge.svg?branch=master\n    :target: https://github.com/django-polymorphic/django-polymorphic-tree/actions/workflows/tests.yaml\n.. image:: https://img.shields.io/pypi/v/django-polymorphic-tree.svg\n    :target: https://pypi.python.org/pypi/django-polymorphic-tree/\n.. image:: https://img.shields.io/pypi/l/django-polymorphic-tree.svg\n    :target: https://pypi.python.org/pypi/django-polymorphic-tree/\n.. image:: https://img.shields.io/codecov/c/github/django-polymorphic/django-polymorphic-tree/master.svg\n    :target: https://codecov.io/github/django-polymorphic/django-polymorphic-tree?branch=master\n\n\nThis package combines django-mptt_ with django-polymorphic_.\nYou can write Django models that form a tree structure where each node can be a different model type.\n\nExample uses:\n\n* Build a tree of organisation and company types (e.g. ``Partner``, ``Reseller``, ``Group`` and ``Customer``)\n* Build a tree of a root node, category nodes, leaf nodes, each with custom fields.\n* Build a todo list of projects, categories and items.\n* Build a book of chapters, sections, and pages.\n\nOrigin\n------\n\nThis code was created in django-fluent-pages_, and extracted to become a separate package.\nThis was done during contract work at Leukeleu_ (known for django-fiber_).\n\n\nInstallation\n============\n\nFirst install the module, preferably in a virtual environment::\n\n    pip install django-polymorphic-tree\n\nOr install the current repository::\n\n    pip install -e git+https://github.com/django-polymorphic/django-polymorphic-tree.git#egg=django-polymorphic-tree\n\nThe main dependencies are django-mptt_ and django-polymorphic_,\nwhich will be automatically installed.\n\nConfiguration\n-------------\n\nNext, create a project which uses the application::\n\n    cd ..\n    django-admin.py startproject demo\n\nAdd the following to ``settings.py``:\n\n.. code:: python\n\n    INSTALLED_APPS += (\n        'polymorphic_tree',\n        'polymorphic',\n        'mptt',\n    )\n\n\nUsage\n-----\n\nThe main feature of this module is creating a tree of custom node types.\nIt boils down to creating a application with 2 files:\n\nThe ``models.py`` file should define the custom node type, and any fields it has:\n\n.. code:: python\n\n    from django.db import models\n    from django.utils.translation import gettext_lazy as _\n    from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey\n\n\n    # A base model for the tree:\n\n    class BaseTreeNode(PolymorphicMPTTModel):\n        parent = PolymorphicTreeForeignKey('self', blank=True, null=True, related_name='children', verbose_name=_('parent'))\n        title = models.CharField(_(\"Title\"), max_length=200)\n\n        class Meta(PolymorphicMPTTModel.Meta):\n            verbose_name = _(\"Tree node\")\n            verbose_name_plural = _(\"Tree nodes\")\n\n\n    # Create 3 derived models for the tree nodes:\n\n    class CategoryNode(BaseTreeNode):\n        opening_title = models.CharField(_(\"Opening title\"), max_length=200)\n        opening_image = models.ImageField(_(\"Opening image\"), upload_to='images')\n\n        class Meta:\n            verbose_name = _(\"Category node\")\n            verbose_name_plural = _(\"Category nodes\")\n\n\n    class TextNode(BaseTreeNode):\n        extra_text = models.TextField()\n\n        # Extra settings:\n        can_have_children = False\n\n        class Meta:\n            verbose_name = _(\"Text node\")\n            verbose_name_plural = _(\"Text nodes\")\n\n\n    class ImageNode(BaseTreeNode):\n        image = models.ImageField(_(\"Image\"), upload_to='images')\n\n        class Meta:\n            verbose_name = _(\"Image node\")\n            verbose_name_plural = _(\"Image nodes\")\n\n\nThe ``admin.py`` file should define the admin, both for the child nodes and parent:\n\n.. code:: python\n\n    from django.contrib import admin\n    from django.utils.translation import gettext_lazy as _\n    from polymorphic_tree.admin import PolymorphicMPTTParentModelAdmin, PolymorphicMPTTChildModelAdmin\n    from . import models\n\n\n    # The common admin functionality for all derived models:\n\n    class BaseChildAdmin(PolymorphicMPTTChildModelAdmin):\n        GENERAL_FIELDSET = (None, {\n            'fields': ('parent', 'title'),\n        })\n\n        base_model = models.BaseTreeNode\n        base_fieldsets = (\n            GENERAL_FIELDSET,\n        )\n\n\n    # Optionally some custom admin code\n\n    class TextNodeAdmin(BaseChildAdmin):\n        pass\n\n\n    # Create the parent admin that combines it all:\n\n    class TreeNodeParentAdmin(PolymorphicMPTTParentModelAdmin):\n        base_model = models.BaseTreeNode\n        child_models = (\n            models.CategoryNode,\n            models.TextNode,  # custom admin allows custom edit/delete view.\n            models.ImageNode,\n        )\n\n        list_display = ('title', 'actions_column',)\n\n        class Media:\n            css = {\n                'all': ('admin/treenode/admin.css',)\n            }\n\n\n    admin.site.register(models.CategoryNode, BaseChildAdmin)\n    admin.site.register(models.TextNode, TextNodeAdmin)\n    admin.site.register(models.ImageNode, BaseChildAdmin)\n    admin.site.register(models.BaseTreeNode, TreeNodeParentAdmin)\n\n\nThe ``child_models`` attribute defines which admin interface is loaded for the *edit* and *delete* page.\nThe list view is still rendered by the parent admin.\n\n\nTests\n-----\n\nTo run the included test suite, execute::\n\n    ./runtests.py\n\nTo test support for multiple Python and Django versions, you need to follow steps below:\n\n* install project requirements in virtual environment\n* install python 2.7, 3.4, 3.5, 3.6 python versions through pyenv (See pyenv (Linux) or Homebrew (Mac OS X).)\n* create .python-version file and add full list of installed versions with which project have to be tested, example::\n\n    2.6.9\n    2.7.13\n    3.4.5\n    3.5.2\n    3.6.0\n* run tox from the repository root::\n\n    pip install tox\n    tox\n\nPython 2.7, 3.4, 3.5 and 3.6 and django 1.8, 1.10 and 1.11 are the currently supported versions.\n\nTodo\n----\n\n* Sphinx Documentation\n\n\nContributing\n------------\n\nThis module is designed to be generic. In case there is anything you didn't like about it,\nor think it's not flexible enough, please let us know. We'd love to improve it!\n\nIf you have any other valuable contribution, suggestion or idea,\nplease let us know as well because we will look into it.\nPull requests are welcome too. :-)\n\n\n.. _Leukeleu: http://www.leukeleu.nl/\n.. _django-fiber: https://github.com/ridethepony/django-fiber\n.. _django-fluent-pages: https://github.com/edoburu/django-fluent-pages\n.. _django-mptt: https://github.com/django-mptt/django-mptt\n.. _django-polymorphic: https://github.com/django-polymorphic/django-polymorphic\n\n\n\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "A polymorphic mptt structure to display content in a tree.",
    "version": "2.1",
    "project_urls": {
        "Download": "https://github.com/django-polymorphic/django-polymorphic-tree/zipball/master",
        "Homepage": "https://github.com/django-polymorphic/django-polymorphic-tree"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "013237fe15ca3ed947d52d720f7fa3015015201131899997d2558e4229228af5",
                "md5": "f18d0492fe1ea5d2a6edf15c0bc4e1c7",
                "sha256": "6e2994ea0d96b0c9e4191e57998e54fcbfafd4362ed2afc877ef61a2548d39f0"
            },
            "downloads": -1,
            "filename": "django_polymorphic_tree-2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f18d0492fe1ea5d2a6edf15c0bc4e1c7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 159151,
            "upload_time": "2021-11-18T10:28:43",
            "upload_time_iso_8601": "2021-11-18T10:28:43.058223Z",
            "url": "https://files.pythonhosted.org/packages/01/32/37fe15ca3ed947d52d720f7fa3015015201131899997d2558e4229228af5/django_polymorphic_tree-2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bb5dbc85eb92c6f28aca811fa534d1f414495ed7bc6d37e6f3b6d2fa36849064",
                "md5": "1c8eb581099c5bf2d18eb69d4adb3faf",
                "sha256": "a6d40c2ef350c5a50bd83727ca8cba85088890a7a08a106caeb3c5dc5ae072aa"
            },
            "downloads": -1,
            "filename": "django-polymorphic-tree-2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "1c8eb581099c5bf2d18eb69d4adb3faf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 144311,
            "upload_time": "2021-11-18T10:28:45",
            "upload_time_iso_8601": "2021-11-18T10:28:45.038497Z",
            "url": "https://files.pythonhosted.org/packages/bb/5d/bc85eb92c6f28aca811fa534d1f414495ed7bc6d37e6f3b6d2fa36849064/django-polymorphic-tree-2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-11-18 10:28:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "django-polymorphic",
    "github_project": "django-polymorphic-tree",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "django-polymorphic-tree"
}
        
Elapsed time: 1.29074s