{"id":13708409,"url":"https://github.com/marshmallow-code/django-rest-marshmallow","last_synced_at":"2025-05-06T13:30:20.128Z","repository":{"id":43974678,"uuid":"42305180","full_name":"marshmallow-code/django-rest-marshmallow","owner":"marshmallow-code","description":"Marshmallow schemas for Django REST framework","archived":true,"fork":false,"pushed_at":"2021-06-07T05:12:48.000Z","size":793,"stargazers_count":213,"open_issues_count":8,"forks_count":14,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-26T21:03:39.828Z","etag":null,"topics":["django","django-rest-marshmallow","marshmallow","rest-api","schema","serialization"],"latest_commit_sha":null,"homepage":"https://marshmallow-code.github.io/django-rest-marshmallow/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marshmallow-code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-11T11:40:23.000Z","updated_at":"2025-03-17T13:22:13.000Z","dependencies_parsed_at":"2022-07-11T00:46:23.062Z","dependency_job_id":null,"html_url":"https://github.com/marshmallow-code/django-rest-marshmallow","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fdjango-rest-marshmallow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fdjango-rest-marshmallow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fdjango-rest-marshmallow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marshmallow-code%2Fdjango-rest-marshmallow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marshmallow-code","download_url":"https://codeload.github.com/marshmallow-code/django-rest-marshmallow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252693468,"owners_count":21789694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-rest-marshmallow","marshmallow","rest-api","schema","serialization"],"created_at":"2024-08-02T23:00:22.067Z","updated_at":"2025-05-06T13:30:19.817Z","avatar_url":"https://github.com/marshmallow-code.png","language":"Python","funding_links":[],"categories":["Serialization","Python"],"sub_categories":["Tools"],"readme":"\u003cdiv class=\"badges\"\u003e\n    \u003ca href=\"http://travis-ci.org/marshmallow-code/django-rest-marshmallow\"\u003e\n        \u003cimg src=\"https://badgen.net/travis/marshmallow-code/django-rest-marshmallow/master\"\n        alt=\"Travis CI\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://pypi.python.org/pypi/django-rest-marshmallow\"\u003e\n        \u003cimg src=\"https://badgen.net/pypi/v/django-rest-marshmallow\"\n        alt=\"django-rest-marshmallow on PyPI\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://marshmallow.readthedocs.io/en/latest/upgrading.html\"\u003e\n        \u003cimg src=\"https://badgen.net/badge/marshmallow/2,3?list=1\"\n        alt=\"marshmallow 3 compatible\"\u003e\n    \u003c/a\u003e\n\u003c/div\u003e\n\n---\n\n# [django-rest-marshmallow](https://marshmallow-code.github.io/django-rest-marshmallow/)\n\n[Marshmallow schemas][marshmallow] for Django REST framework.\n\n---\n\n## Overview\n\n`django-rest-marshmallow` provides an alternative serializer implementation to the built-in serializers, by using the python [marshmallow] library, but exposing the same API as REST framework's `Serializer` class.\n\n## Requirements\n\n* Python (3.6+)\n* Django REST framework (3.8+)\n* Marshmallow (3.0.0+)\n\n## Installation\n\nInstall using `pip`...\n\n```bash\n$ pip install django-rest-marshmallow\n```\n\n---\n\n## Usage\n\nDefine your schemas as you would with marshmallow, but importing the `Schema` class from `rest_marshmallow` instead.\n\n```python\nfrom rest_marshmallow import Schema, fields\n\nclass CustomerSchema(Schema):\n    name = fields.String()\n    email = fields.Email()\n    created_at = fields.DateTime()\n```\n\nThe Schema class has the same interface as a Django REST framework serializer, so you can use it in your generic views...\n\n```python\nclass CustomerListView(generics.ListAPIView):\n    queryset = Customer.objects.all()\n    serializer_class = CustomerSchema\n```\n\nOr use the serializer API directly, for either serialization...\n\n```python\nserializer = CustomerSchema(queryset, many=True)\nreturn Response(serializer.data)\n```\n\nOr for validation...\n\n```python\nserializer = CustomerSchema(data=request.data)\nserializer.is_valid(raise_exception=True)\nserializer.validated_data\n```\n\n#### Instance create and update\n\nIf you want to support `serializer.save()` you'll need to define the `.create()` and/or `.update()` methods explicitly.\n\n```python\nclass CustomerSchema(Schema):\n    name = fields.String()\n    email = fields.Email()\n    created_at = fields.DateTime()\n\n    def create(self, validated_data):\n        return Customer.objects.create(**validated_data)\n\n    def update(self, instance, validated_data):\n        for key, value in validated_data.items():\n            setattr(instance, key, value)\n        instance.save()\n        return instance\n```\n\nYou can now use `.save()` from your view code…\n\n```python\nserializer = CustomerSchema(data=request.data)\nserializer.is_valid(raise_exception=True)\nserializer.save()\nreturn Response(serializer.data, status=status.HTTP_201_CREATED)\n```\n\nOr use the schema together with generic views that create or update instances...\n\n```python\nclass CustomerListView(generics.ListCreateAPIView):\n    queryset = Customer.objects.all()\n    serializer_class = CustomerSchema\n```\n\nNote that you should always use the `create()` and `update()` methods instead of overriding the `make_object()` marshmallow method.\n\n#### Nested representations\n\nFor nested representations, use marshmallow's standard `Nested` field as usual.\n\n```python\nfrom rest_marshmallow import fields, Schema\n\nclass ArtistSchema(Schema):\n    name = fields.String()\n\nclass AlbumSchema(Schema):\n    title = fields.String()\n    release_date = fields.Date()\n    artist = fields.Nested(ArtistSchema)\n```\n\n#### Excluding fields\n\nThe marshmallow `only` and `exclude` arguments are also valid as serializer arguments:\n\n```python\nserializer = CustomerSchema(queryset, many=True, only=('name', 'email'))\nreturn Response(serializer.data)\n```\n\n---\n\n## Testing\n\nInstall testing requirements.\n\n```bash\n$ pip install -r requirements.txt\n```\n\nRun with runtests.\n\n```bash\n$ ./runtests.py\n```\n\nYou can also use the excellent [tox](http://tox.readthedocs.org/en/latest/) testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:\n\n```bash\n$ tox\n```\n\n## Documentation\n\nTo build the documentation, you'll need to install `mkdocs`.\n\n```bash\n$ pip install mkdocs\n```\n\nTo preview the documentation:\n\n```bash\n$ mkdocs serve\nRunning at: http://127.0.0.1:8000/\n```\n\nTo build the documentation:\n\n```bash\n$ mkdocs build\n```\n\n\n[marshmallow]: https://marshmallow.readthedocs.org/en/latest/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarshmallow-code%2Fdjango-rest-marshmallow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarshmallow-code%2Fdjango-rest-marshmallow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarshmallow-code%2Fdjango-rest-marshmallow/lists"}