Skip to main content

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.

Project details


Download files

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

Source Distribution

django-janyson-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

File details

Details for the file django-janyson-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-janyson-0.1.1.tar.gz
Algorithm Hash digest
SHA256 58e532064ed0d1af7b5e7074051cda7851cd07d54ddd905a3c6503d4ceedfde5
MD5 cf22110eb6d0d16928c81e58b9b0c617
BLAKE2b-256 fe7d1905bc8a40d5304e7c81b1c54c93839a6c0f465cc96ff49ade8ecc99ab92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page