Skip to content

Instantly share code, notes, and snippets.

@rsudip90 rsudip90/views.py
Last active May 12, 2019

Embed
What would you like to do?
drf serializer class dynamic -- views.py -- using serializer_action_classes
from rest_framework import viewsets
from .mixins import GetSerializerClassMixin
from .models import User, Company, SystemUserRole
from .serializers import (
CompanySerializer,
CompanyDetailSerializer,
UserSerializer,
UserDetailSerializer,
)
class CompanyViewSet(GetSerializerClassMixin, viewsets.ModelViewSet):
"""
API endpoint that allows companies to be viewed or edited.
"""
queryset = Company.objects.all()
serializer_class = CompanyDetailSerializer
serializer_action_classes = {
'list': CompanySerializer,
}
filterset_fields = ("country", "state", "city", )
search_fields = ("name", "email", )
ordering_fields = ("name", "country", )
ordering = ("-created_at", )
class UserViewSet(GetSerializerClassMixin, viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all()
serializer_class = UserDetailSerializer
serializer_action_classes = {
'list': UserSerializer,
}
filterset_fields = ("country", "state", "city", "zipcode", "company", )
search_fields = ("first_name", "last_name", "email", )
ordering_fields = ("first_name", "last_name", "email", )
ordering = ("-created_at", )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.