{"id":13813455,"url":"https://github.com/allisson/django-rest-framework-serializer-mixins","last_synced_at":"2025-05-15T00:33:15.848Z","repository":{"id":57421645,"uuid":"121507146","full_name":"allisson/django-rest-framework-serializer-mixins","owner":"allisson","description":"Mixins for Django Rest Framework Serializer","archived":false,"fork":false,"pushed_at":"2019-02-18T01:11:10.000Z","size":12,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-02T06:52:14.030Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/allisson.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGES.rst","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":"2018-02-14T12:15:06.000Z","updated_at":"2022-08-23T16:54:34.000Z","dependencies_parsed_at":"2022-09-10T13:21:54.824Z","dependency_job_id":null,"html_url":"https://github.com/allisson/django-rest-framework-serializer-mixins","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-serializer-mixins","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-serializer-mixins/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-serializer-mixins/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allisson%2Fdjango-rest-framework-serializer-mixins/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allisson","download_url":"https://codeload.github.com/allisson/django-rest-framework-serializer-mixins/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224929825,"owners_count":17393934,"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":[],"created_at":"2024-08-04T04:01:18.329Z","updated_at":"2024-11-19T08:30:37.251Z","avatar_url":"https://github.com/allisson.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"django-rest-framework-serializer-mixins\n=======================================\n\n.. image:: https://travis-ci.org/allisson/django-rest-framework-serializer-mixins.svg?branch=master\n    :target: https://travis-ci.org/allisson/django-rest-framework-serializer-mixins\n\n.. image:: https://codecov.io/gh/allisson/django-rest-framework-serializer-mixins/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/allisson/django-rest-framework-serializer-mixins\n\n.. image:: https://img.shields.io/pypi/v/djangorestframework-serializer-mixins.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-serializer-mixins\n\n.. image:: https://img.shields.io/github/license/allisson/django-rest-framework-serializer-mixins.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-serializer-mixins\n\n.. image:: https://img.shields.io/pypi/pyversions/djangorestframework-serializer-mixins.svg\n        :target: https://pypi.python.org/pypi/djangorestframework-serializer-mixins\n\n\nMixins for Django Rest Framework Serializer\n\nHow to install\n--------------\n\n.. code:: shell\n\n    pip install djangorestframework-serializer-mixins\n\nHow to use DynamicFieldsMixin\n-----------------------------\n\nAssume you have a Post model:\n\n.. code:: python\n\n    # testapp/models.py\n    from django.conf import settings\n    from django.db import models\n\n\n    class Post(models.Model):\n        user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='posts')\n        title = models.CharField(max_length=128)\n        body = models.TextField()\n        created_at = models.DateTimeField(auto_now_add=True)\n        updated_at = models.DateTimeField(auto_now=True)\n\n        def __str__(self):\n            return self.title\n\n        class Meta:\n            ordering = ['-created_at']\n\n\nWrite DynamicPostSerializer with DynamicFieldsMixin:\n\n.. code:: python\n\n    # testapp/serializers.py\n    from rest_framework import serializers\n\n    from rest_framework_serializer_mixins import DynamicFieldsMixin\n\n    from .models import Post\n\n\n    class DynamicPostSerializer(DynamicFieldsMixin, serializers.ModelSerializer):\n        class Meta:\n            model = Post\n            fields = (\n                'body',\n                'created_at',\n                'id',\n                'title',\n                'updated_at',\n                'user',\n            )\n            read_only_fields = (\n                'id',\n                'user',\n            )\n\nNow, you can define fields and read_only_field like this:\n\n.. code:: python\n\n    \u003e\u003e\u003e from django.contrib.auth.models import User\n    \u003e\u003e\u003e from testapp.models import Post\n    \u003e\u003e\u003e from testapp.serializers import DynamicPostSerializer\n    \u003e\u003e\u003e user = User.objects.create_user('user', 'user@email.com', '123456')\n    \u003e\u003e\u003e post = Post.objects.create(user=user, title='My Title', body='My Body')\n    \u003e\u003e\u003e serializer = DynamicPostSerializer(post) # return fields and read_only_fields from Meta\n    \u003e\u003e\u003e serializer.data\n    {'body': 'My Body', 'created_at': '2018-02-14T14:15:29.772209Z', 'id': 1, 'title': 'My Title', 'updated_at': '2018-02-14T14:15:29.772312Z', 'user': 1}\n    \u003e\u003e\u003e serializer = DynamicPostSerializer(post, fields=('title', 'body')) # return only title and body fields\n    \u003e\u003e\u003e serializer.data\n    {'body': 'My Body', 'title': 'My Title'}\n    \u003e\u003e\u003e serializer = DynamicPostSerializer(post, read_only_fields=('title', 'body'), data={'title': 'New Title', 'body': 'New Body'}) # set title and body as read_only_fields\n    \u003e\u003e\u003e serializer.is_valid()\n    True\n    \u003e\u003e\u003e serializer.save()\n    \u003cPost: My Title\u003e\n    \u003e\u003e\u003e serializer.data\n    {'body': 'My Body', 'created_at': '2018-02-14T14:15:29.772209Z', 'id': 1, 'title': 'My Title', 'updated_at': '2018-02-14T14:19:14.838001Z', 'user': 1} # title and body don't change\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallisson%2Fdjango-rest-framework-serializer-mixins","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallisson%2Fdjango-rest-framework-serializer-mixins","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallisson%2Fdjango-rest-framework-serializer-mixins/lists"}