Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A simple and lightweight implementation of materialized path tree structures in Django.

Project description

Django Materialized Paths

A simple and lightweight implementation of materialized path tree structures in Django.

Installation

To install, run:

pip install django-materialized-paths

Configuration

Add the following into the INSTALLED_APPS of your projects:

INSTALLED_APPS = (
 ...
 'django-materialized-paths',
)

Usage

To use, extend the BaseNode into your model:

from django-materialized-paths import BaseNode

class FolderExample(BaseNode):
    """My folder class"""
    name = models.CharField(max_length=48)

The model overrides Django's django.db.models.Model save() method to automatically manage changes to inheritance:

from .models import FolderExample # concrete class from above

root = FolderExample.objects.create(name="root")
child = FolderExample.objects.create(name="child", parent=root)
grandchild = FolderExample.objects.create(name="child", parent=child)

For example, to convert grandchild to a root:

grandchild.parent = None
grandchild.save()

Usage - Properties and Inherited

BaseNode.depth # Root is 0
BaseNode.parent
BaseNode.children # Only returns direct descendants

BaseNode.save() # Set the parent field to automatically manage hierarchy
BaseNode.delete() # NOTE: The BaseNode.parent field is set to cascade, so deleting a parent will delete all children 

Usage - Methods

Note that these methods generally return querysets

# Accessors

BaseNode.get_root()
BaseNode.get_ancestor(depth=int)
BaseNode.get_ancestors() 
BaseNode.get_descendants() 
BaseNode.get_siblings() 

# Convenience

BaseNode.has_children()
BaseNode.is_child_of(parent_id=int)

# ID-based accessors - computed directly from path without accessing db

BaseNode.get_ancestors_ids()
BaseNode.get_descendants_ids()

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-materialized-paths, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size django_materialized_paths-0.1-py3-none-any.whl (5.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-materialized-paths-0.1.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page