Skip to main content

Django rest framework extension to handle writable nested fields

Project description

drf-nest

Build Status PyPI

Writable nested serialisers for Django Rest Framework

The sample

A sample project is included which implements a retail type use case.

  • Sales have a foreign key to a store
  • Sale items have foreign keys to the sale.

To run sample:

pip install -r requirements.txt
python manage.py migrate
python manage.py createsuperuser super super@super.com super
python manage.py runserver

To use

python setup.py install

In your serialisers.py file import extensions:

from drf_nest.serializers import ExtendedHyperlinkedSerialiser
from drf_nest.serializer_fields import ExtendedModelSerialiserField

For each model serialiser using nested field use the ExtendedHyperlinkedSerialiser

class SaleSerialiser(ExtendedHyperlinkedSerialiser):

For each nested representation in the parent object use the ExtendedModelSerialiserField

class SaleSerialiser(ExtendedHyperlinkedSerialiser):
    sale_items = ExtendedModelSerialiserField(
        SaleItemSerialiser(), 
        many=True, 
        required=False, 
        allow_null=True)

We won't know the foreign key to fulfill the relationship if we are creating the parent so in each sub objects serialiser, the parent object must be made optional.

class SaleItemSerialiser(ExtendedHyperlinkedSerialiser):
    sale = serializers.HyperlinkedRelatedField(
        required=False,
        view_name='sale-detail',
        queryset=Sale.objects.all()
    )

How does it work

The serialiser field overrides the to internal function to return a dictionary (or list of dictionaries) rather than the django model instance. This is done because the fields do not know if the parent exists already but may have a required foreign key constraint.

Features

See the sample project tests for example POST requests.

  • During POST, PUT and PATCH user can specify nested object either by URL or full serialised representation
  • Adds type field to allow for generic foreign keys (in development)
  • Serialisation of model with foreign key
  • Serialisation of model with reverse relationship
  • Serialisation of model with many to many relationship

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_nest-0.1a5.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

drf_nest-0.1a5-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file drf_nest-0.1a5.tar.gz.

File metadata

  • Download URL: drf_nest-0.1a5.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for drf_nest-0.1a5.tar.gz
Algorithm Hash digest
SHA256 960969e98438edd61643cb6fd446934ce7c64bef7fd79ac6fb3e228c65e10cf0
MD5 caaeaf07a3494fd4750ba1c2706034f5
BLAKE2b-256 d4776a2c03d39715690807ef4997002b890ed384abeac088fb610b2b68041202

See more details on using hashes here.

File details

Details for the file drf_nest-0.1a5-py3-none-any.whl.

File metadata

  • Download URL: drf_nest-0.1a5-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for drf_nest-0.1a5-py3-none-any.whl
Algorithm Hash digest
SHA256 b30e4bce10c383a26fda522c70cacdb1633ee6af3189b017585c76a359d2a409
MD5 170adb408c6d599238bc7d50e7365103
BLAKE2b-256 aef8979443e0a08ded8d4298920fb496990bd3876fc037fe0e8a07d1357da42a

See more details on using hashes here.

Supported by

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