Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Virtual model fields that are transparently mapped to Postgres jsonb

Project Description

Store additional model fields as JSON object in PostgreSQL’s jsonb field and work with them as regular model fields. Need new boolean/text/foreign key/many-to-many/etc. field? Just add the decorator with the field description to your model. It’s all! No more annoying migrations.

Installation

  • Install the package using pip install django-janyson.
  • Add janyson to INSTALLED_APPS setting.

Requirements

  • Python 2 or 3 (tested with 2.7+ and 3.3+)
  • Django 1.9+ with psycopg2
  • six

Example

from django.db import models

from janyson.decorators import add_fields
from janyson.fields import JanySONField


class Tag(models.Model):

    name = models.CharField(max_length=16)

    def __str__(self):
        return "[Tag: {}]".format(self.name)


extra_fields = {
    'desc': {'type': 'str'},
    'qty': {'type': 'num', 'default': 0, 'use_default': True},
    'avail': {'type': 'nullbool', 'use_default': True},
    'main_tag': {'type': 'fk', 'model': Tag},
    'tags': {'type': 'm2m', 'model': 'demo_app.Tag'},
}

common_options = {
    'use_default': False,
    'dir_hide': True,
}

@add_fields(extra_fields, field_options=common_options, janyson_field='extra')
class Item(models.Model):

    name = models.CharField(max_length=64)
    extra = JanySONField(verbose_name='janyson field', default=dict,
                         blank=True, null=True)

    def __str__(self):
        return "[Item: {}]".format(self.name)
>>> from demo_app.models import Tag, Item

>>> Tag.objects.create(name='tag1')
>>> Tag.objects.create(name='tag2')
>>> item = Item(name='test')

>>> item
<Item: [Item: test]>
>>> item.desc
AttributeError: 'Item' object has no attribute 'desc'
>>> item.qty
0
>>> print(item.avail)
None
>>> item.tags
AttributeError: 'Item' object has no attribute 'tags'

>>> tags = Tag.objects.all()
>>> item.desc = 'description'
>>> item.qty = 100
>>> item.avail = True
>>> item.tags = tags
>>> item.save()

>>> del item
>>> item = Item.objects.get(name='test')
>>> item.desc
'description'
>>> item.qty
100
>>> item.avail
True
>>> item.tags
[<Tag: [Tag: tag1]>, <Tag: [Tag: tag1]>, <Tag: [Tag: tag2]>]

Tests

python runtests.py [-d TESTDBNAME] [-h HOSTNAME] [-p PORT] [-U USERNAME] [-P PASSWORD]

Run python runtests.py --help for additional info.

Documentation

Coming soon.

Release History

Release History

This version
History Node

0.1.1

History Node

0.1.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-janyson-0.1.1.tar.gz (10.0 kB) Copy SHA256 Checksum SHA256 Source May 27, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting