GithubHelp home page GithubHelp logo

murphyke / modeltree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chop-dbhi/modeltree

0.0 2.0 0.0 370 KB

Django model relation tree traversal, i.e. automatic joins.

Home Page: http://modeltree.harvest.io

modeltree's Introduction

ModelTree

Build Status Coverage Status Bitdeli Badge

ModelTree is a layer that sits atop of the Django ORM providing APIs for dynamically generating QuerySets at runtime. It manages figuring out all necessary joins between tables based on the relationships defined in each Model. The models below are all related in some way:

Install

Install using Pip or easy_install:

pip install modeltree

Setup

Add it to INSTALLED_APPS in your project settings:

INSTALLED_APPS = (
    ...
    'modeltree',
)

Define a default modeltree in your project settings:

MODELTREES = {
    'default': {
        'model': 'myapp.SomeModel',
    }
}

See how it looks by previewing the traversal tree:

python manage.py modeltree preview

The API

Let's start with a few example models:

from django.db import models

class Office(models.Model):
    location = models.CharField(max_length=50)


class Title(models.Model):
    name = models.CharField(max_length=50)
    salary = models.IntegerField()


class Employee(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    title = models.ForeignKey(Title)
    office = models.ForeignKey(Office)
    is_manager = models.BooleanField(default=False)


class Project(models.Model):
    name = models.CharField(max_length=50)
    employees = models.ManyToManyField(Employee)
    manager = models.OneToOneField(Employee, related_name='managed_projects')
    due_date = models.DateField()


class Meeting(models.Model):
    attendees = models.ManyToManyField(Employee)
    project = models.ForeignKey(Project, null=True)
    office = models.ForeignKey(Office)
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

In most cases the way relationships are traversed (the SQL joins that occurs) are predictable. Thus we can determine the shortest "path" between models.

>>> from modeltree.tree import trees
>>> from modeltree.utils import print_traversal_tree
>>> mt = trees.create(Office)
>>> mt
<ModelTree for Office>

>>> print_traversal_tree(mt)
Office
....Employee (via employee_set)
........Project (via project_set)
........Title (via title)
....Meeting (via meeting_set)

The hierarchy above shows the relationships relative to the Office model. We can easily query relative to the Office model.

Let's ask the question, "show me all the offices that have employees with the salary greater than 50,000". We need to use a special class here inspired by the Q class:

>>> from modeltree.utils import M
>>> m = M(mt, title__salary__gt=50000)
>>> queryset = Office.objects.filter(m)
>>> str(queryset.query)
'SELECT "tests_office"."id", "tests_office"."location" FROM "tests_office" INNER
JOIN "tests_employee" ON ("tests_office"."id" = "tests_employee"."office_id")
INNER JOIN "tests_title" ON ("tests_employee"."title_id" = "tests_title"."id")
WHERE "tests_title"."salary" > 50000 '

The lookup keyword argument title__salary__gt=50000 is a bit different than the normal lookups. This syntax is <app>__<model>__<field>__<operator>=<value>. The <app> and <operator> portions are optional. <app> is only required if there is an ambigiously named model, i.e. two apps have a model of the same name.

Since we are supplying the ModelTree instance with the lookup, it knows to build the QuerySet relative to the Office model.

modeltree's People

Contributors

bruth avatar naegelyd avatar

Watchers

 avatar  avatar

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.