Francesco Frassinelli
Francesco Frassinelli

Reputation: 3375

Removing fields from django-rest-framework view

is there any way to show only a list of fields or excluding some of them when using django-rest-framework?

Here's my app/views.py:

from rest_framework.generics import ListAPIView
from .models import PhpbbUsers

class UsersReadView(ListAPIView):
    model = PhpbbUsers

Obiously there are some user information that I don't want to show to everyone. How could I do?

Solution code

from rest_framework import generics, serializers
from .models import PhpbbUsers

class UsersSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = PhpbbUsers
        fields = ('user_id', 'username', 'user_avatar')

class UsersReadView(generics.ListAPIView):
    model = PhpbbUsers
    serializer_class = UsersSerializer

Upvotes: 1

Views: 1296

Answers (1)

Tom Christie
Tom Christie

Reputation: 33901

Set the serializer_class attribute on the view.

See the quickstart for a good example: http://django-rest-framework.org/tutorial/quickstart.html

Upvotes: 1

Related Questions