Coder Social home page Coder Social logo

django-qjoin's Introduction

django-qjoin

Welcome to the documentation for django-qjoin!

Django-qjoin is an experimental package that provides a way of adding arbitrary joins to a query using expressions. This is very much a work in progress and isn't currently working on the latest Django versions.

Examples

Given the models:

class Person(models.Model):
    """Simple Person model."""

    FEMALE = 'F'
    MALE = 'M'

    SEX_CHOICES = (
        (FEMALE, 'Female'),
        (MALE, 'Male'),
    )

    name = models.CharField(max_length=255)
    age = models.PositiveSmallIntegerField(default=0)
    sex = models.CharField(max_length=1, choices=SEX_CHOICES)


class Email(models.Model):
    """A Person may have one or more emails. Emails are not necessarily tied to people."""
    email = models.EmailField()
    person = models.ForeignKey(Person, null=True)
    primary = models.BooleanField(default=False)


class Bounce(models.Model):
    """Simple model to track an email bounce."""
    email = models.EmailField()
    bounced = models.DateTimeField(auto_now_add=True)

A simple join:

PersonAddress.objects.filter(QJoin(person=JoinExpression(Person)))

Any join can be converted in to a left outer join:

PersonAddress.objects.filter(QJoin(person=JoinExpression(Person, outer=True)))

Joining on a non related field:

join = QJoin(email=JoinExpression(
    Email.objects.filter(primary=True, email='[email protected]'),
    'email')
)
bounces = Bounce.objects.filter(bounced__gt='2014-01-01').filter(join)

Running the Tests

You can run the tests with via:

python setup.py test

or:

python runtests.py

django-qjoin's People

Contributors

manfre avatar mlavin avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

django-qjoin's Issues

Non-FK Join

It should be possible to create a join between two on columns which don't have an existing FK.

Functional Tests

I expect the SQL tests will be too fragile particularly across Django versions. I created QJoinTestCase to hold functional tests for the queries but haven't written any yet.

Outer Join

It should be possible to change a join to an outer join.

Does not work on Django 1.11

>>> from qjoin.models import QJoin, JoinExpression
ImportError: cannot import name 'ExpressionNode'

Looks like Django 1.11 changed ExpressionNode location (if still exists)
from django.db.models.expressions import ExpressionNode

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.