jhoelzer / mptt-hierarchy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mptt-hierarchy

Django MPTT: Hierarchical Data and You

Sometimes when categorizing data, there are potentially infinite ways that the data can be organized. For example, a file path for an operating system includes all the parent folders and the the location of the item itself, like this:

/Users/jkaufeld/demo/test.py

If we're looking at an object called test.py and we want to find all of the "parent nodes", or the folders that contain this object, then we have to do a recursive call upwards. In a 'traditional' Django ORM setup, a naive example might look something like this:

def get_complete_path(item): complete_path = list()

while True: item = item.parent complete_path.prepend(item.path) if item.path == '/': break

return os.path.join(complete_path) This results in a database call for every single layer, which works out to three calls to get the complete path of our mythical object. We can do a lot better though... what if we could get that info in one call?

Using something called Modified Preorder Tree Traversal (MPTT), we can treat all our data like a gigantic tree, with a single starting point (the "root" object) and parents, siblings, and children galore. We can get the same data as above with a single call:

item.get_ancestors()

The goal of this assignment is to learn about this type of database and different ways of working with it. Build a simple Django server that uses MPTT models from django-mptt to create a Dropbox-esque web interface where you can create "folders" and "files" in an arbitrary structure and then display that structure. We won't actually be uploading anything, just making model instances and using them to represent real data. See the rubric for more detailed information. Submit as link to your repo on Github.

To Run This App

  • Clone/download this code
  • Setup virtual environment
$pipenv install
  • Start the virtual environment
$pipenv shell
  • Start the server
$python3 manage.py runserver

About


Languages

Language:Python 93.4%Language:HTML 6.6%