GithubHelp home page GithubHelp logo

akaariai / djorm-ext-pgarray Goto Github PK

View Code? Open in Web Editor NEW

This project forked from niwinz/djorm-pgarray

0.0 3.0 0.0 350 KB

PostgreSQL native array fields extension for Django.

Home Page: http://www.niwi.be/2012/10/07/postgresql-array-fields-with-django/

License: Other

djorm-ext-pgarray's Introduction

djorm-ext-pgarray

PostgreSQL array field for Django.

Introduction

Django by default, has a large collection of possible types that can be used to define the model. But sometimes we need to use some more complex types offered by PostgreSQL. In this case, we will look the integrating of PostgreSQL array with Django.

Django version support: 1.5, 1.6, 1.7

Quickstart

djorm-ext-pgarray exposes a simple django model field djorm_pgarray.fields.ArrayField. This accepts two additional parameters: dbtype that represents a postgresql type, and dimension that represents a dimension of array field.

This is a sample definition of model using a ArrayField:

from django.db import models
from djorm_pgarray.fields import ArrayField

class Register(models.Model):
    name = models.CharField(max_length=200)
    points = ArrayField(dbtype="int")

class Register2(models.Model):
    name = models.CharField(max_length=200)
    texts = ArrayField(dbtype="text", dimension=2) # this creates `points text[][]` postgresql field.

Creating objects

This is a sample example of creating objects with array fields.

>>> Register.objects.create(points = [1,2,3,4])
<Register: Register object>
>>> Register2.objects.create(texts = [['Hello', 'World'], ['Hola', 'Mundo']])
<Register2: Register2 object>

Using custom PostgreSQL data types

Some times we need use some other data types that postgresql offers for make arrays and djorm-ext-pgarray does not offers builtin support for it. Now, djorm-ext-pgarray supports a simple way to extend it:

class Register(models.Model):
    name = models.CharField(max_length=200)
    macs = ArrayField(dbtype="macaddr", type_cast=lambda x: str(x))
    # Same as previous line but uses str as callback directly
    # macs = ArrayField(dbtype="macaddr", type_cast=str)
    objects = ExpressionManager()

If type_cast patameter is not None, ArrayField ignores the no existence of a builtin cast function for some type and use a function passed throught type_cast argument.

How install it?

You can clone the repo from github and install with simple python setup.py install command. Or use a pip, for install it from Python Package Index.

pip install djorm-ext-pgarray

Additionally, you can install djorm-ext-expressions, that can help with complex queries using array fields.

djorm-ext-pgarray's People

Contributors

apollo13 avatar btubbs avatar carljm avatar chrisrodz avatar derekleverenz avatar dstufft avatar fdemmer avatar jessecrocker avatar nigma avatar philipplaeubli avatar ryankask avatar temoto avatar

Watchers

 avatar  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.