Skip to main content

DRF library to operate resource's properties as a dictionary

Project description

drf-madprops Build Status

DRF library of helpers to operate on lists of resource properties as dicts

Written and used by the folks at Yola to support our free website builder.

Overview

It's a typical case for relational DBs when some table (e.g. User) is extended via subordinate key-value table (e.g. UserPreference). This allows to dynamically add/delete fields to User (stored in UserPreference). Usually those property models have very simple structures, e.g:

class UserPreference(models.Model):
    id = models.AutoField()
    user = models.ForeignKey(User, related_name='preferences')
    name = models.CharField()
    value = models.CharField)

But it's not very convenient to expose/operate on them via standard DRF serializers. We'll get something like:

[
    {"id": "id1", "user": "user1", "name": "property1", "value": "value1"},
    {"id": "id2", "user": "user1", "name": "property2", "value": "value2"}
    ...
]

This library contains two base classes for property's serializers (for cases when properties are exposed as separate resource and as nested resource) which allows to retrieve/operate on parent resource properties as dicts. For example, instead of representation listed above, we'll get something like:

{
    "property1": "value1",
    "property2": "value2",
    ...
}

Usage

Additional meta options

  • read_only_props: list of property names, which values cannot be changed via serializer.
  • json_props: list of property names, which values are stored as JSON. Serializer will json.loads() those values when converting to representation and will json.dumps() them before saving.

As a nested serializer

from madprops.serializers import NestedPropertySerializer, PropertiesOwnerSerializer


class PreferenceSerializer(NestedPropertySerializer):
    class Meta:
        model = Preference
        read_only_props = ('user_token', 'tutorial_email_sent')
        json_props = ('packages',)


class UserSerializer(PropertiesOwnerSerializer):
    preferences = PreferenceSerializer(many=True, required=False)

As a serializer used for properties endpoint

from madprops.serializers import PropertySerializer


class PreferenceSerializer(PropertySerializer):
    class Meta:
        model = Preference
        read_only_props = ('user_token', 'tutorial_email_sent')


class PreferencePrivateViewSet(ModelViewSet):
    serializer_class = PreferenceSerializer
    paginate_by = None

Testing

Run the tests with:

tox

Or install development requirements and run tests with:

pip install -r requirements.txt
pytest tests

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

drf-madprops-1.2.0.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file drf-madprops-1.2.0.tar.gz.

File metadata

  • Download URL: drf-madprops-1.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for drf-madprops-1.2.0.tar.gz
Algorithm Hash digest
SHA256 be90a937dbc6fec2e76f135f8eb5f2ad4a2fe2e19228b581f99a0ea9d24cd5ab
MD5 0f246475687fcce56c418bbe3bff65ef
BLAKE2b-256 2ba828e6acb6b1d236c1dade9ecf79455de8332b19fedd1afb4c035d46558af8

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