Skip to main content

adding restriction of your api's return fields, in restframework

Project description

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)

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)),
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-returnfields-0.0.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file django-returnfields-0.0.tar.gz.

File metadata

File hashes

Hashes for django-returnfields-0.0.tar.gz
Algorithm Hash digest
SHA256 0dc28b6c2d274b442bed7b371e89d00af4601885e2df49f597c87cd37c701ae5
MD5 5d3fa38f66a5181a5961fcbb53f9e158
BLAKE2b-256 296e7313e918689b72b687bd4b2c52670780596f5e2c7093579c0fa6ceb6925c

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page