drf-serializer-dumps


Namedrf-serializer-dumps JSON
Version 1.0.3 PyPI version JSON
download
home_pagehttps://github.com/Friskes/drf-serializer-dumps
SummaryDecorator for creating dict based on the drf serializer class for swagger
upload_time2024-05-02 17:18:15
maintainerNone
docs_urlNone
authorFriskes
requires_python>=3.8
licenseMIT License Copyright (c) 2024 Friskes Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords django drf spectacular swagger
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Decorator for creating dict based on the drf serializer class for swagger

<div align="center">

| Project   |     | Status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
|-----------|:----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| CI/CD     |     | [![Latest Release](https://github.com/Friskes/drf-serializer-dumps/actions/workflows/publish-to-pypi.yml/badge.svg)](https://github.com/Friskes/drf-serializer-dumps/actions/workflows/publish-to-pypi.yml)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| Quality   |     | [![Coverage](https://codecov.io/github/Friskes/drf-serializer-dumps/graph/badge.svg?token=vKez4Pycrc)](https://codecov.io/github/Friskes/drf-serializer-dumps)                                                                                                                                                                                                                                                                                                                               |
| Package   |     | [![PyPI - Version](https://img.shields.io/pypi/v/drf-serializer-dumps?labelColor=202235&color=edb641&logo=python&logoColor=edb641)](https://badge.fury.io/py/drf-serializer-dumps) ![PyPI - Support Python Versions](https://img.shields.io/pypi/pyversions/drf-serializer-dumps?labelColor=202235&color=edb641&logo=python&logoColor=edb641) ![Project PyPI - Downloads](https://img.shields.io/pypi/dm/drf-serializer-dumps?logo=python&label=downloads&labelColor=202235&color=edb641&logoColor=edb641)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| Meta      |     | [![types - Mypy](https://img.shields.io/badge/types-Mypy-202235.svg?logo=python&labelColor=202235&color=edb641&logoColor=edb641)](https://github.com/python/mypy) [![License - MIT](https://img.shields.io/badge/license-MIT-202235.svg?logo=python&labelColor=202235&color=edb641&logoColor=edb641)](https://spdx.org/licenses/) [![code style - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/format.json&labelColor=202235)](https://github.com/astral-sh/ruff) |

</div>

> Provides a decorator for converting the drf serializer class to a dictionary

## Install
1. Install package
    ```bash
    pip install drf-serializer-dumps
    ```

## About decorator
`serializer_dumps` decorator is based on the assignment of fields in the serializer to generate a dict, for `SerializerMethodField`, the definition is made by `OpenApiTypes` or the usual python type hints.

- Optional parameters:
    - `exclude_fields` Exclude a number of serializer fields when generating a dictionary.
    - `renew_type_value` Generate a new `uuid` and `datetime, date, time` when calling the function.
    - `extend_type_map` Expand the type dictionary to default values, new types and their values, or redefine existing types and their values.

## Usage example

### Example 1
```python
from rest_framework import serializers
from drf_serializer_dumps.decorators import serializer_dumps


class PersonCars(serializers.Serializer):
    car_name = serializers.CharField()
    car_price = serializers.IntegerField()


class PersonSerializer(serializers.Serializer):
    name = serializers.CharField()
    age = serializers.IntegerField()
    cars = PersonCars(many=True)


result = serializer_dumps(PersonSerializer)
print(result)
>>> {'name': 'string', 'age': 1, 'cars': [{'car_name': 'string', 'car_price': 1}]}
```

### Example 2
```python
from rest_framework import serializers
from django.contrib.postgres.fields import ArrayField
from drf_serializer_dumps.decorators import serializer_dumps


class Person(models.Model):
    name = models.CharField(max_length=256)
    phones = ArrayField(models.CharField(max_length=256))


class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person
        fields = '__all__'


result = serializer_dumps(PersonSerializer)
print(result)
>>> {'id': 1, 'name': 'string', 'phones': ['string']}
```

### Example 3
> Integration with drf-spectacular extend_schema decorator
```python
@extend_schema(
    examples=[
        OpenApiExample('Name1', serializer_dumps(Some1Serializer)),
        OpenApiExample('Name2', serializer_dumps(Some2Serializer)),
    ]
)
def your_api_method(self, request, *args, **kwargs):
    ...
```

## Contributing
We would love you to contribute to `drf-serializer-dumps`, pull requests are very welcome! Please see [CONTRIBUTING.md](https://github.com/Friskes/drf-serializer-dumps/blob/main/CONTRIBUTING.md) for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Friskes/drf-serializer-dumps",
    "name": "drf-serializer-dumps",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "Django, DRF, Spectacular, Swagger",
    "author": "Friskes",
    "author_email": "Friskes <friskesx@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/75/ee/e60e511892e901d828dadf98032df761315f584e22b1f782ce147abcafd7/drf_serializer_dumps-1.0.3.tar.gz",
    "platform": null,
    "description": "# Decorator for creating dict based on the drf serializer class for swagger\n\n<div align=\"center\">\n\n| Project   |     | Status                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n|-----------|:----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| CI/CD     |     | [![Latest Release](https://github.com/Friskes/drf-serializer-dumps/actions/workflows/publish-to-pypi.yml/badge.svg)](https://github.com/Friskes/drf-serializer-dumps/actions/workflows/publish-to-pypi.yml)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |\n| Quality   |     | [![Coverage](https://codecov.io/github/Friskes/drf-serializer-dumps/graph/badge.svg?token=vKez4Pycrc)](https://codecov.io/github/Friskes/drf-serializer-dumps)                                                                                                                                                                                                                                                                                                                               |\n| Package   |     | [![PyPI - Version](https://img.shields.io/pypi/v/drf-serializer-dumps?labelColor=202235&color=edb641&logo=python&logoColor=edb641)](https://badge.fury.io/py/drf-serializer-dumps) ![PyPI - Support Python Versions](https://img.shields.io/pypi/pyversions/drf-serializer-dumps?labelColor=202235&color=edb641&logo=python&logoColor=edb641) ![Project PyPI - Downloads](https://img.shields.io/pypi/dm/drf-serializer-dumps?logo=python&label=downloads&labelColor=202235&color=edb641&logoColor=edb641)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |\n| Meta      |     | [![types - Mypy](https://img.shields.io/badge/types-Mypy-202235.svg?logo=python&labelColor=202235&color=edb641&logoColor=edb641)](https://github.com/python/mypy) [![License - MIT](https://img.shields.io/badge/license-MIT-202235.svg?logo=python&labelColor=202235&color=edb641&logoColor=edb641)](https://spdx.org/licenses/) [![code style - Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/format.json&labelColor=202235)](https://github.com/astral-sh/ruff) |\n\n</div>\n\n> Provides a decorator for converting the drf serializer class to a dictionary\n\n## Install\n1. Install package\n    ```bash\n    pip install drf-serializer-dumps\n    ```\n\n## About decorator\n`serializer_dumps` decorator is based on the assignment of fields in the serializer to generate a dict, for `SerializerMethodField`, the definition is made by `OpenApiTypes` or the usual python type hints.\n\n- Optional parameters:\n    - `exclude_fields` Exclude a number of serializer fields when generating a dictionary.\n    - `renew_type_value` Generate a new `uuid` and `datetime, date, time` when calling the function.\n    - `extend_type_map` Expand the type dictionary to default values, new types and their values, or redefine existing types and their values.\n\n## Usage example\n\n### Example 1\n```python\nfrom rest_framework import serializers\nfrom drf_serializer_dumps.decorators import serializer_dumps\n\n\nclass PersonCars(serializers.Serializer):\n    car_name = serializers.CharField()\n    car_price = serializers.IntegerField()\n\n\nclass PersonSerializer(serializers.Serializer):\n    name = serializers.CharField()\n    age = serializers.IntegerField()\n    cars = PersonCars(many=True)\n\n\nresult = serializer_dumps(PersonSerializer)\nprint(result)\n>>> {'name': 'string', 'age': 1, 'cars': [{'car_name': 'string', 'car_price': 1}]}\n```\n\n### Example 2\n```python\nfrom rest_framework import serializers\nfrom django.contrib.postgres.fields import ArrayField\nfrom drf_serializer_dumps.decorators import serializer_dumps\n\n\nclass Person(models.Model):\n    name = models.CharField(max_length=256)\n    phones = ArrayField(models.CharField(max_length=256))\n\n\nclass PersonSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Person\n        fields = '__all__'\n\n\nresult = serializer_dumps(PersonSerializer)\nprint(result)\n>>> {'id': 1, 'name': 'string', 'phones': ['string']}\n```\n\n### Example 3\n> Integration with drf-spectacular extend_schema decorator\n```python\n@extend_schema(\n    examples=[\n        OpenApiExample('Name1', serializer_dumps(Some1Serializer)),\n        OpenApiExample('Name2', serializer_dumps(Some2Serializer)),\n    ]\n)\ndef your_api_method(self, request, *args, **kwargs):\n    ...\n```\n\n## Contributing\nWe would love you to contribute to `drf-serializer-dumps`, pull requests are very welcome! Please see [CONTRIBUTING.md](https://github.com/Friskes/drf-serializer-dumps/blob/main/CONTRIBUTING.md) for more information.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Friskes  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Decorator for creating dict based on the drf serializer class for swagger",
    "version": "1.0.3",
    "project_urls": {
        "Changelog": "https://github.com/Friskes/drf-serializer-dumps/releases/",
        "Homepage": "https://github.com/Friskes/drf-serializer-dumps",
        "Issues": "https://github.com/Friskes/drf-serializer-dumps/issues"
    },
    "split_keywords": [
        "django",
        " drf",
        " spectacular",
        " swagger"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "748748b97b8982ca265db0ac0db9a33db075b6b7597c1872c3bd494c037767a2",
                "md5": "2a7de278de201cef816b44ea893f3a51",
                "sha256": "a6d0540110997b1fd641e1cdf8809da1772d36384baf5a6a4aebf07f1c6289ce"
            },
            "downloads": -1,
            "filename": "drf_serializer_dumps-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2a7de278de201cef816b44ea893f3a51",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8107,
            "upload_time": "2024-05-02T17:18:13",
            "upload_time_iso_8601": "2024-05-02T17:18:13.707221Z",
            "url": "https://files.pythonhosted.org/packages/74/87/48b97b8982ca265db0ac0db9a33db075b6b7597c1872c3bd494c037767a2/drf_serializer_dumps-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "75eee60e511892e901d828dadf98032df761315f584e22b1f782ce147abcafd7",
                "md5": "055ef591a56fc20f434ec9f3509a39b2",
                "sha256": "287082c570e370c2d865e403676e85dc0e9275a23c8673834aeac6ed579a8e59"
            },
            "downloads": -1,
            "filename": "drf_serializer_dumps-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "055ef591a56fc20f434ec9f3509a39b2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 17220,
            "upload_time": "2024-05-02T17:18:15",
            "upload_time_iso_8601": "2024-05-02T17:18:15.097142Z",
            "url": "https://files.pythonhosted.org/packages/75/ee/e60e511892e901d828dadf98032df761315f584e22b1f782ce147abcafd7/drf_serializer_dumps-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-02 17:18:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Friskes",
    "github_project": "drf-serializer-dumps",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "drf-serializer-dumps"
}
        
Elapsed time: 0.25999s