Skip to main content

Nested Django Serializer

An updated version of wadofstuff that is compatible with python3 and Django 4. Used to fully serialize Django model foreign key and many-to-many relations.

Installation

The package is available on pypi and can be installed via

$ pip install nested-django-serializer

To use the package in your Django project, simply add the following variable to your settings.py

SERIALIZATION_MODULES = {
  'json': 'nested_django_serializer.django.serializers.json'
}

Features

  • relations - a list or dictionary of model related fields to be followed and serialized.
  • extras - a list of non-model field properties or callables to be serialized.
  • excludes - a list of fields to be excluded from serialization. The excludes list takes precedence over the fields argument.

Relations

The Nested Django Serializer has the ability to fully serialize referenced Django models as opposed to the default behaviour of simply returning the primary key value.

To override the default behaviour, supply the relations keyword argument to serializers.serialize()

The argument should either be a list of relations to be serialized or a dictionary of key/value pairs where the keys are relations to be serialized and the values are lists of arguments to be passed to the serializer when serializing that relation.

serializers.serialize('json', Group.objects.all(), indent=4, relations=('permissions',))
[
    {
        "pk": 2,
        "model": "auth.group",
        "fields": {
            "name": "session",
            "permissions": [
                {
                    "pk": 19,
                    "model": "auth.permission",
                    "fields": {
                        "codename": "add_session",
                        "name": "Can add session",
                        "content_type": 7
                    }
                }
            ]
        }
    }
]
serializers.serialize('json', Group.objects.all(), indent=4, 
                      relations={'permissions': {'fields': ('content_type', ), 'relations': ('content_type', )}})
[
    {
        "pk": 2,
        "model": "auth.group",
        "fields": {
            "name": "session",
            "permissions": [
                {
                    "pk": 19,
                    "model": "auth.permission",
                    "fields": {
                        "content_type": {
                            "pk": 7,
                            "model": "contenttypes.contenttype",
                            "fields": {
                                "model": "session",
                                "name": "session",
                                "app_label": "sessions"
                            }
                        }
                    }
                }
            ]
        }
    }
]

Release files for nested-django-serializer 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nested-django-serializer 1.1.0
File Size Uploaded
nested_django_serializer-1.1.0.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nested-django-serializer 1.1.0
File Interpreter ABI Platform
nested_django_serializer-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.7 kB

Release files / nested_django_serializer-1.1.0.tar.gz

Download URL nested_django_serializer-1.1.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2c4b03858b8e2a41a7825d013abae5e6cde4448f7693b231ea6218550a386c31
BLAKE2b-256 checksum
How to use checksums
a6b5a6196530fafb02eb3afc4524ef68956094ab555d36fa6d5ad432c281a51e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release files / nested_django_serializer-1.1.0-py3-none-any.whl

Download URL nested_django_serializer-1.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c2da0f4ee16e101242a65f5e6a702ed86472b9d05c5eca878cbdcc461563d4f
BLAKE2b-256 checksum
How to use checksums
e2e31ef5a59ea7960d26778f256b5d1dc9c1aa7a8d5a5d20cbf327ef4810b057
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page