nested-django-serializer


Namenested-django-serializer JSON
Version 1.1.0 PyPI version JSON
download
home_page
SummaryModifies the Django serializer such that it fully serializes foreign key relations.
upload_time2023-06-02 17:38:06
maintainer
docs_urlNone
authorColton Schneider
requires_python>=3.8
licenseMIT
keywords django json serializer
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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

```python
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.

```python
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
                    }
                }
            ]
        }
    }
]
```

```python
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"
                            }
                        }
                    }
                }
            ]
        }
    }
]

```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "nested-django-serializer",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "django json serializer",
    "author": "Colton Schneider",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/a6/b5/a6196530fafb02eb3afc4524ef68956094ab555d36fa6d5ad432c281a51e/nested_django_serializer-1.1.0.tar.gz",
    "platform": null,
    "description": "\n# Nested Django Serializer\n\nAn updated version of wadofstuff that is compatible with python3 and Django 4. Used to fully serialize Django model\nforeign key and many-to-many relations.\n\n## Installation\n\nThe package is available on pypi and can be installed via\n\n`$ pip install nested-django-serializer`\n\nTo use the package in your Django project, simply add the following variable to your settings.py\n\n```python\nSERIALIZATION_MODULES = {\n  'json': 'nested_django_serializer.django.serializers.json'\n}\n```\n\n## Features\n\n- relations - a list or dictionary of model related fields to be followed\n  and serialized.\n- extras - a list of non-model field properties or callables to be\n  serialized.\n- excludes - a list of fields to be excluded from serialization. The\n  excludes list takes precedence over the fields argument.\n\n### Relations\nThe Nested Django Serializer has the ability to fully serialize referenced Django models as opposed to the default \nbehaviour of simply returning the primary key value. \n\nTo override the default behaviour, supply the `relations` keyword argument to `serializers.serialize()` \n\nThe argument should either be a list of relations to be serialized or a dictionary of key/value pairs where the keys are\nrelations to be serialized and the values are lists of arguments to be passed to the serializer when serializing that \nrelation.\n\n```python\nserializers.serialize('json', Group.objects.all(), indent=4, relations=('permissions',))\n[\n    {\n        \"pk\": 2,\n        \"model\": \"auth.group\",\n        \"fields\": {\n            \"name\": \"session\",\n            \"permissions\": [\n                {\n                    \"pk\": 19,\n                    \"model\": \"auth.permission\",\n                    \"fields\": {\n                        \"codename\": \"add_session\",\n                        \"name\": \"Can add session\",\n                        \"content_type\": 7\n                    }\n                }\n            ]\n        }\n    }\n]\n```\n\n```python\nserializers.serialize('json', Group.objects.all(), indent=4, \n                      relations={'permissions': {'fields': ('content_type', ), 'relations': ('content_type', )}})\n[\n    {\n        \"pk\": 2,\n        \"model\": \"auth.group\",\n        \"fields\": {\n            \"name\": \"session\",\n            \"permissions\": [\n                {\n                    \"pk\": 19,\n                    \"model\": \"auth.permission\",\n                    \"fields\": {\n                        \"content_type\": {\n                            \"pk\": 7,\n                            \"model\": \"contenttypes.contenttype\",\n                            \"fields\": {\n                                \"model\": \"session\",\n                                \"name\": \"session\",\n                                \"app_label\": \"sessions\"\n                            }\n                        }\n                    }\n                }\n            ]\n        }\n    }\n]\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Modifies the Django serializer such that it fully serializes foreign key relations.",
    "version": "1.1.0",
    "project_urls": {
        "Github": "https://github.com/colton305/nested-django-serializer"
    },
    "split_keywords": [
        "django",
        "json",
        "serializer"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2e31ef5a59ea7960d26778f256b5d1dc9c1aa7a8d5a5d20cbf327ef4810b057",
                "md5": "0b9f50632aa4b3a897c8c921f5af080b",
                "sha256": "6c2da0f4ee16e101242a65f5e6a702ed86472b9d05c5eca878cbdcc461563d4f"
            },
            "downloads": -1,
            "filename": "nested_django_serializer-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0b9f50632aa4b3a897c8c921f5af080b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7604,
            "upload_time": "2023-06-02T17:38:04",
            "upload_time_iso_8601": "2023-06-02T17:38:04.077019Z",
            "url": "https://files.pythonhosted.org/packages/e2/e3/1ef5a59ea7960d26778f256b5d1dc9c1aa7a8d5a5d20cbf327ef4810b057/nested_django_serializer-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6b5a6196530fafb02eb3afc4524ef68956094ab555d36fa6d5ad432c281a51e",
                "md5": "d56b13d5fe92d4b4eafe32606c6c453e",
                "sha256": "2c4b03858b8e2a41a7825d013abae5e6cde4448f7693b231ea6218550a386c31"
            },
            "downloads": -1,
            "filename": "nested_django_serializer-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d56b13d5fe92d4b4eafe32606c6c453e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6119,
            "upload_time": "2023-06-02T17:38:06",
            "upload_time_iso_8601": "2023-06-02T17:38:06.292408Z",
            "url": "https://files.pythonhosted.org/packages/a6/b5/a6196530fafb02eb3afc4524ef68956094ab555d36fa6d5ad432c281a51e/nested_django_serializer-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-02 17:38:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "colton305",
    "github_project": "nested-django-serializer",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "nested-django-serializer"
}
        
Elapsed time: 0.07022s