Decorator for creating dict based on the drf serializer class for swagger
Project description
Decorator for creating dict based on the drf serializer class for swagger
Provides a decorator for converting the drf serializer class to a dictionary
Install
- Install package
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 newuuid
anddatetime, 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
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
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
@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 for more information.
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_serializer_dumps-1.0.3.tar.gz
(17.2 kB
view hashes)
Built Distribution
Close
Hashes for drf_serializer_dumps-1.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 287082c570e370c2d865e403676e85dc0e9275a23c8673834aeac6ed579a8e59 |
|
MD5 | 055ef591a56fc20f434ec9f3509a39b2 |
|
BLAKE2b-256 | 75eee60e511892e901d828dadf98032df761315f584e22b1f782ce147abcafd7 |
Close
Hashes for drf_serializer_dumps-1.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6d0540110997b1fd641e1cdf8809da1772d36384baf5a6a4aebf07f1c6289ce |
|
MD5 | 2a7de278de201cef816b44ea893f3a51 |
|
BLAKE2b-256 | 748748b97b8982ca265db0ac0db9a33db075b6b7597c1872c3bd494c037767a2 |