Last active
February 14, 2022 07:56
-
-
Save rsudip90/59b86c8825f05c425d6fd5877e98985c to your computer and use it in GitHub Desktop.
drf serializer class dynamic -- views.py -- using serializer_action_classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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