https://github.com/friskes/drf-serializer-dumps
Decorator for creating dict based on the drf serializer class for swagger
https://github.com/friskes/drf-serializer-dumps
decorator django django-rest-framework documentation-generator drf-spectacular schema serializers swagger
Last synced: about 1 month ago
JSON representation
Decorator for creating dict based on the drf serializer class for swagger
- Host: GitHub
- URL: https://github.com/friskes/drf-serializer-dumps
- Owner: Friskes
- License: mit
- Created: 2024-04-21T18:19:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-21T19:23:12.000Z (about 1 year ago)
- Last Synced: 2024-10-31T10:51:31.789Z (7 months ago)
- Topics: decorator, django, django-rest-framework, documentation-generator, drf-spectacular, schema, serializers, swagger
- Language: Python
- Homepage:
- Size: 36.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Decorator for creating dict based on the drf serializer class for swagger
| Project | | Status |
|-----------|:----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| CI/CD | | [](https://github.com/Friskes/drf-serializer-dumps/actions/workflows/publish-to-pypi.yml) |
| Quality | | [](https://codecov.io/github/Friskes/drf-serializer-dumps) |
| Package | | [](https://badge.fury.io/py/drf-serializer-dumps)   |
| Meta | | [](https://github.com/python/mypy) [](https://spdx.org/licenses/) [](https://github.com/astral-sh/ruff) |> 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_dumpsclass 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_dumpsclass 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.