adding restriction of your api’s return fields, in restframework.
what is this?
The api is defined, such as below.
request: GET /users/1/?format=json
status code: 200
response: {
"id": 1,
"url": "http://testserver/users/1/?format=json",
"username": "foo",
"email": "",
"is_staff": false,
"skills": [
{
"id": 1,
"user": 1,
"name": "magic"
},
{
"id": 2,
"user": 1,
"name": "magik"
}
]
}
If you passing return_fields option, api’s response is filtered.
request: GET /users/1/?format=json&return_fields=username,id
status code: 200
response: {
"id": 1,
"username": "foo"
}
And adding skip__fields option, treated as ignored fields.
request: GET /users/1/?format=json&return_fields=username,skills&skip_fields=skills__id,skills__user
status code: 200
response: {
"username": "foo",
"skills": [
{
"name": "magic"
},
{
"name": "magik"
}
]
}
how to use it?
using django_returnfields.serializer_factory
from rest_framework import viewsets
from django_returnfields import serializer_factory
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = serializer_factory(UserSerializer)
appendix
if you requesting with aggressive option, then, django_returnfields tries to use Query.defer() or Query.only(). e.g.
GET /users/1/?format=json&return_fields=username,skills&skip_fields=skills__id,skills__user&aggressive=1
aggressive option is not only using defer and only, but also semi-automatic join or prefetching (TODO: introduction)
example
## models
from django.db import models
from django.contrib.auth.models import User
class Skill(models.Model):
name = models.CharField(max_length=255, default="", null=False)
user = models.ForeignKey(User, null=False, related_name="skills")
## serializers
from rest_framework import serializers
class SkillSerializer(serializers.ModelSerializer):
class Meta:
model = Skill
fields = ('id', 'user', 'name')
class UserSerializer(serializers.ModelSerializer):
skills = SkillSerializer(many=True, read_only=True)
class Meta:
model = User
fields = ('id', 'url', 'username', 'email', 'is_staff', 'skills')
## viewsets
from rest_framework import viewsets
from django_returnfields import serializer_factory
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = serializer_factory(UserSerializer)
class SkillViewSet(viewsets.ModelViewSet):
queryset = Skill.objects.all()
serializer_class = serializer_factory(SkillSerializer)
## routes
router = routers.DefaultRouter()
router.register(r'users', viewsets.UserViewSet)
router.register(r'skills', viewsets.SkillViewSet)
urlpatterns = [
url(r'^api/', include(router.urls)),
]
0.3.2
simplify
bug fix, a optimization is not worked with a serializer has many=False option.
0.3.1
supporting source option of django restframework’s field
0.3
rewriting aggressive implementation.
Metadata
Release files for django-returnfields 0.3.2
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 |
|---|---|---|---|---|
| django-returnfields-0.3.2.tar.gz | 9.2 kB | Without provenance | Details for django-returnfields-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_returnfields-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for django_returnfields-0.3.2-py3-none-any.whl |
Total release size: 21.7 kB
Release files / django-returnfields-0.3.2.tar.gz
| Download URL | django-returnfields-0.3.2.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ffedc1338c2fd23cecdeb99e35d39a639705647922e8f5385b76c0307b8e356
|
|
BLAKE2b-256 checksum How to use checksums |
cc9178445f02464f3d70c34e6cac3145fcdb873816c7717e12c827e6d0fa3a80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_returnfields-0.3.2-py3-none-any.whl
| Download URL | django_returnfields-0.3.2-py3-none-any.whl |
|---|---|
| Size | 12.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0cd3ce6604204d657b550152642f4a6c774ba15318d8a5b8f1457a2d5beeb619
|
|
BLAKE2b-256 checksum How to use checksums |
9d77536a061fca61b2d0b265cb6a3796815b1be204ddaab9c8e8613df8fa4b1e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-returnfields 0.3.2
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.