GithubHelp home page GithubHelp logo

shaunduncan / django-postgres Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zacharyvoase/django-postgres

0.0 2.0 0.0 102 KB

First-class Postgres feature support for the Django ORM.

Home Page: http://django-postgres.readthedocs.org/

License: The Unlicense

django-postgres's Introduction

django-postgres

Adds first-class support for PostgreSQL features to the Django ORM.

Planned features include:

Obviously this is quite a large project, but I think it would provide a huge amount of value to Django developers.

Why?

PostgreSQL is an excellent data store, with a host of useful and efficiently-implemented features. Unfortunately these features are not exposed through Django's ORM, primarily because the framework has to support several SQL backends and so can only provide a set of features common to all of them.

The features made available here replace some of the following practices:

  • Manual denormalization on save() (such that model saves may result in three or more separate queries).
  • Sequences represented by a one-to-many, with an order integer field.
  • Complex types represented by JSON in a text field.

Example

The following represents a whirlwind tour of potential features of the project:

from django.db import models
import django_postgres as pg


USStates = pg.Enum('states_of_the_usa', ['AL', ..., 'WY'])


class Address(pg.CompositeType):
    line1 = models.CharField(max_length=100)
    line2 = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=100)
    zip_code = models.CharField(max_length=10)
    state = USStates()
    country = models.CharField(max_length=100)


class USPhoneNumber(pg.Domain):
    data_type = models.CharField(max_length=10)
    constraints = [
      r"VALUE ~ '^\d{3}-?\d{3}-?\d{4}$'"
    ]


class Customer(models.Model):
    name = models.CharField(max_length=100)
    shipping_address = Address()
    telephone_numbers = pg.Array(USPhoneNumber())
    is_preferred = models.BooleanField(default=False)


class PreferredCustomer(pg.View):
    projection = ['myapp.Customer.*']
    sql = """SELECT * FROM myapp_customer WHERE is_preferred = TRUE;"""

The SQL produced by this might look like:

CREATE TYPE states_of_the_usa AS ENUM ('AL', ..., 'WY');

CREATE TYPE myapp_address AS (
    line1 varchar(100),
    line2 varchar(100),
    city varchar(100),
    zip_code varchar(10),
    state states_of_the_usa,
    country varchar(100)
);

CREATE DOMAIN myapp_usphonenumber AS varchar(10)
    CHECK(VALUE ~ '^\d{3}-?\d{3}-?\d{4}$');

CREATE TABLE myapp_customer (
    id SERIAL PRIMARY KEY,
    shipping_address myapp_address,
    telephone_numbers myapp_usphonenumber[]
);

CREATE VIEW myapp_preferredcustomer AS
SELECT * FROM myapp_customer WHERE is_preferred = TRUE;

To create all your views, run python manage.py sync_pgviews

django-postgres's People

Contributors

scott-w avatar zacharyvoase 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.