{ "info": { "author": "Oxan van Leeuwen", "author_email": "oxan@oxanvanleeuwen.nl", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3.7", "Topic :: Internet :: WWW/HTTP", "Topic :: Software Development :: Libraries :: Application Frameworks", "Topic :: Software Development :: Libraries :: Python Modules", "Typing :: Typed" ], "description": "Dataclasses serializer\n======================\n\nA `dataclasses `__ serializer for the `Django REST Framework\n`__.\n\n.. image:: https://travis-ci.org/oxan/djangorestframework-dataclasses.svg?branch=master\n :target: https://travis-ci.org/oxan/djangorestframework-dataclasses\n.. image:: https://badge.fury.io/py/djangorestframework-dataclasses.svg\n :target: https://badge.fury.io/py/djangorestframework-dataclasses\n\n\nRequirements\n------------\n\n* Python (3.7+)\n* Django (2.0, 2.1, 2.2)\n* Django REST Framework (3.9, 3.10)\n\nThese are the supported Python and package versions. Older versions will probably work as well, but haven't been tested\nby the author.\n\nInstallation\n------------\n\n::\n\n $ pip install djangorestframework-dataclasses\n\nBasic usage\n-----------\n\nThe package provides the ``DataclassSerializer`` serializer, defined in the ``rest_framework_dataclasses.serializers``\nnamespace.\n\n.. code:: Python\n\n from rest_framework_dataclasses.serializers import DataclassSerializer\n\nThis serializer provides a shortcut that lets you automatically create a ``Serializer`` class with fields that\ncorrespond to the fields on a dataclass. In usage, the ``DataclassSerializer`` is the same as a regular ``Serializer``\nclass, except that:\n\n* It will automatically generate fields for you, based on the declaration in the dataclass.\n* To make this possible it requires that a ``dataclass`` property is specified in the ``Meta`` subclass, with as value\n a dataclass that has type annotations.\n* It includes simple default implementations of ``.create()`` and ``.update()``.\n\nFor example, define a dataclass as follows:\n\n.. code:: Python\n\n @dataclass\n class Person:\n name: str\n email: str\n alive: bool\n birth_date: typing.Optional[datetime.date]\n phone: typing.List[str]\n movie_ratings: typing.Dict[str, int]\n\n def age(self) -> int:\n return datetime.date.today().year - self.birth_date.year\n\nThe serializer for this dataclass can now trivially be defined without having to duplicate all fields:\n\n.. code:: Python\n\n class PersonSerializer(DataclassSerializer):\n class Meta:\n dataclass = Person\n\n # is equivalent to\n class PersonSerializer(Serializer):\n name = fields.CharField()\n email = fields.CharField()\n alive = fields.BooleanField()\n birth_date = fields.DateField(allow_null=True)\n phone = fields.ListField(child=fields.CharField())\n movie_ratings = fields.DictField(child=fields.IntegerField())\n\nNote that this usage pattern is very similar to that of the built-in ``ModelSerializer``. This is intentional, with the\nwhole API modelled after that of ``ModelSerializer``.\n\nYou can add extra fields or override default fields by declaring them explicitly on the class, just as you would for a\nregular ``Serializer`` class. This allows to specify extra options or change the field type.\n\n.. code:: Python\n\n class PersonSerializer(Serializer):\n email = fields.EmailField()\n\n class Meta:\n dataclass = Person\n\nCustomize field generation\n--------------------------\n\nTo customize the generated fields, the ``DataclassSerializer`` accepts the following options in the ``Meta`` class. All\noptions have the same behaviour as the identical options in ``ModelSerializer``.\n\n* ``dataclass`` specifies the type of dataclass used by the serializer. This is equivalent to the ``model`` option in\n ``ModelSerializer``.\n\n* ``fields`` and ``exclude`` can be used to specify which fields should respectively be included and excluded in the\n serializer. These cannot both be specified.\n\n The ``fields`` option accepts the magic value ``__all__`` to specify that all fields on the dataclass should be used.\n This is also the default value, so it is not mandatory to specify either ``fields`` or ``exclude``.\n\n* ``read_only_fields`` can be used to mark a subset of fields as read-only.\n\n* ``extra_kwargs`` can be used to specify arbitrary additional keyword arguments on fields. This can be useful to\n extend or change the autogenerated field without explicitly declaring the field on the serializer. This option should\n be a dictionary, mapping field names to a dictionary of keyword arguments.\n\n If the autogenerated field is a composite field (a list or dictionary), the arguments are applied to the composite\n field. To add keyword arguments to the composite fields child field (that is, the field used for the items in the\n list or dictionary) list), they should be specified as a nested dictionary under the ``child_kwargs`` name.\n\n .. code:: Python\n\n class PersonSerializer(DataclassSerializer):\n class Meta:\n extra_kwargs = {\n 'height': { 'decimal_places': 1 },\n 'movie_ratings': { 'child_kwargs': { 'min_value': 0, 'max_value': 10 } }\n }\n\n* ``validators`` functionality is unchanged.\n\n* ``depth`` (as known from ``ModelSerializer``) is not yet supported.\n\nNesting and models\n------------------\n\nIf your dataclass has a field that contains a dataclass instance as well, the ``DataclassSerializer`` will\nautomatically create another ``DataclassSerializer`` for that field, so that its value will be nested. This also works\nfor dataclasses contained in lists or dictionaries, or even several layers deep.\n\n.. code:: Python\n\n @dataclass\n class House:\n address: str\n owner: Person\n residents: typing.List[Person]\n\n class HouseSerializer(DataclassSerializer):\n class Meta:\n dataclass = House\n\nThis will serialize as:\n\n.. code:: Python\n\n >>> serializer = HouseSerializer(instance=house)\n >>> serializer.data\n {\n 'address': 'Main Street 5',\n 'owner': { 'name': 'Alice' }\n 'residents': [\n { 'name': 'Alice', 'email': 'alice@example.org', ... },\n { 'name': 'Bob', 'email': 'bob@example.org', ... },\n { 'name': 'Charles', 'email': 'charles@example.org', ... }\n ]\n }\n\nThis does not give the option to customize the field generation of the nested dataclasses. If that is needed, you\nshould declare the serializer to be used explicitly.\n\nLikewise, if your dataclass has a field that contains a Django model, the ``DataclassSerializer`` will automatically\ngenerate a relational field for you.\n\n.. code:: Python\n\n class Company(models.Model):\n name = models.CharField()\n\n @dataclass\n class Person:\n name: str\n employer: Company\n\nThis will serialize as:\n\n.. code:: Python\n\n >>> serializer = PersonSerializer(instance=user)\n >>> print(repr(serializer))\n PersonSerializer():\n name = fields.CharField()\n employer = fields.PrimaryKeyRelatedField(queryset=Company.objects.all())\n >>> serializer.data\n {\n \"name\": \"Alice\",\n \"employer\": 1\n }\n\nIf you want to nest the model in the serialized representation, you should specify the model serializer to be used by\ndeclaring the field explicitly.\n\nIf you prefer to use hyperlinks to represent relationships rather than primary keys, in the same package you can find\nthe ``HyperlinkedDataclassSerializer`` class: it generates a ``HyperlinkedRelatedField`` instead of a\n``PrimaryKeyRelatedField``.\n\nAdvanced usage\n--------------\n\n* The output of methods or properties on the dataclass can be included as a (read-only) field in the serialized state\n by adding their name to the ``fields`` option in the ``Meta`` class.\n\n* If you don't need to customize the generated fields, ``DataclassSerializer`` can also be used directly without\n creating a subclass. In that case, the dataclass should be specified using the ``dataclass`` constructor parameter:\n\n .. code:: Python\n\n serializer = DataclassSerializer(data=request.data, dataclass=Person)\n\nField mappings\n--------------\n\nSo far, field generation is supported for the following types and their subclasses:\n\n* ``str``, ``bool``, ``int`` and ``float``.\n* ``date``, ``datetime``, ``time`` and ``timedelta`` from the ``datetime`` package.\n* ``decimal.Decimal`` (requires specifying ``max_digits`` and ``decimal_places`` through ``extra_kwargs``).\n* ``typing.Iterable`` (including ``typing.List``).\n* ``typing.Mapping`` (including ``typing.Dict``).\n* ``django.db.Model``\n\nFor advanced users, the ``DataclassSerializer`` also exposes an API that you can override in order to alter how\nserializer fields are generated:\n\n* The ``serializer_field_mapping`` property contains a dictionary that maps types to REST framework serializer classes.\n You can override or extend this mapping to change the serializer field classes that are used for fields based on\n their type.\n\n* The ``serializer_related_field`` is the serializer field class that is used for relations to models.\n\n* The ``build_unknown_typed_field()`` method is called to create serializer field classes for types that it does not\n understand. By default this throws an error, but you can extend this with custom logic to create serializer fields.\n\n* The ``build_standard_field()``, ``build_relational_field()``, ``build_nested_field()`` and ``build_property_field()``\n methods are used to process respectively fields, embedded models, embedded dataclasses and properties. These can be\n overridden to change the field generation logic, but at that point it's usually a better idea to just declare the\n field explicitly.", "description_content_type": "text/x-rst", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/oxan/djangorestframework-dataclasses", "keywords": "", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "djangorestframework-dataclasses", "package_url": "https://pypi.org/project/djangorestframework-dataclasses/", "platform": "", "project_url": "https://pypi.org/project/djangorestframework-dataclasses/", "project_urls": { "Homepage": "https://github.com/oxan/djangorestframework-dataclasses" }, "release_url": "https://pypi.org/project/djangorestframework-dataclasses/0.2/", "requires_dist": null, "requires_python": ">=3.7", "summary": "A dataclasses serializer for Django REST Framework", "version": "0.2" }, "last_serial": 5851441, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "57453f88eb4f6b1f222e6d8f9ed4cc29", "sha256": "127efb82d6167bad30e72c965f5cd1c85d9b47c1dfea56ec053fc9be7b96e0c9" }, "downloads": -1, "filename": "djangorestframework-dataclasses-0.1.tar.gz", "has_sig": false, "md5_digest": "57453f88eb4f6b1f222e6d8f9ed4cc29", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.7", "size": 12039, "upload_time": "2019-09-09T20:39:08", "url": "https://files.pythonhosted.org/packages/d3/fb/7c47ef66c83d2610ed66e5dad6c560197f430259287627ff91f30be21953/djangorestframework-dataclasses-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "c0f0d62072338c35a95d2ed4628fface", "sha256": "6d9519c2beba9d708ddfe5f753ee5e4d44d45ce8e4e3f6ed2378b2df8e824374" }, "downloads": -1, "filename": "djangorestframework-dataclasses-0.2.tar.gz", "has_sig": false, "md5_digest": "c0f0d62072338c35a95d2ed4628fface", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.7", "size": 14087, "upload_time": "2019-09-18T16:57:31", "url": "https://files.pythonhosted.org/packages/25/70/3761481f6d18b745271d4ff79c2a7bf060ee3fdd8445912ea8c720f417a5/djangorestframework-dataclasses-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c0f0d62072338c35a95d2ed4628fface", "sha256": "6d9519c2beba9d708ddfe5f753ee5e4d44d45ce8e4e3f6ed2378b2df8e824374" }, "downloads": -1, "filename": "djangorestframework-dataclasses-0.2.tar.gz", "has_sig": false, "md5_digest": "c0f0d62072338c35a95d2ed4628fface", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.7", "size": 14087, "upload_time": "2019-09-18T16:57:31", "url": "https://files.pythonhosted.org/packages/25/70/3761481f6d18b745271d4ff79c2a7bf060ee3fdd8445912ea8c720f417a5/djangorestframework-dataclasses-0.2.tar.gz" } ] }