django-rest-framework-serializer-mixins
Mixins for Django Rest Framework Serializer
How to install
pip install djangorestframework-serializer-mixins
How to use
Assume you have a Post model:
# testapp/models.py
from django.conf import settings
from django.db import models
class Post(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='posts')
title = models.CharField(max_length=128)
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-created_at']
Write DynamicPostSerializer with DynamicFieldsMixin:
# testapp/serializers.py
from rest_framework import serializers
from rest_framework_serializer_mixins import DynamicFieldsMixin
from .models import Post
class DynamicPostSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
class Meta:
model = Post
fields = (
'body',
'created_at',
'id',
'title',
'updated_at',
'user',
)
read_only_fields = (
'id',
'user',
)
Now, you can define fields and read_only_field like this:
>>> from django.contrib.auth.models import User
>>> from testapp.models import Post
>>> from testapp.serializers import DynamicPostSerializer
>>> user = User.objects.create_user('user', 'user@email.com', '123456')
>>> post = Post.objects.create(user=user, title='My Title', body='My Body')
>>> data = DynamicPostSerializer(post) # return fields and read_only_fields from Meta
>>> serializer = DynamicPostSerializer(post) # return fields and read_only_fields from Meta
>>> serializer.data
{'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}
>>> serializer = DynamicPostSerializer(post, fields=('title', 'body')) # return only title and body fields
>>> serializer.data
{'body': 'My Body', 'title': 'My Title'}
>>> serializer = DynamicPostSerializer(post, read_only_fields=('title', 'body'), data={'title': 'New Title', 'body': 'New Body'}) # set title and body as read_only_fields
>>> serializer.is_valid()
True
>>> serializer.save()
<Post: My Title>
>>> serializer.data
{'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
Metadata
Release files for djangorestframework-serializer-mixins 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| djangorestframework-serializer-mixins-0.1.0.tar.gz | 4.2 kB | Without provenance | Details for djangorestframework-serializer-mixins-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| djangorestframework_serializer_mixins-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for djangorestframework_serializer_mixins-0.1.0-py3-none-any.whl |
Total release size: 9.7 kB
Release files / djangorestframework-serializer-mixins-0.1.0.tar.gz
| Download URL | djangorestframework-serializer-mixins-0.1.0.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d70d4609c1b68302f26ac3aff65c5b9486ac759753acb0f4fa0af35e29bc4ddd
|
|
BLAKE2b-256 checksum How to use checksums |
3dab7578d5ce4f0459d83b762f67855179b5ce12bdf4f78597ddcc19a8e8e3cf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / djangorestframework_serializer_mixins-0.1.0-py3-none-any.whl
| Download URL | djangorestframework_serializer_mixins-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b12e4bd547ee6c8514f9a50c96c59a4a77aa223f78068f2db7dc704ea234f94a
|
|
BLAKE2b-256 checksum How to use checksums |
b388047ac2ed9bcaeed1a24c545421d1414314573752be0daa867ec962046796
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for djangorestframework-serializer-mixins 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.