from apps.user.models.users import User
class Team(models.Model):
user = models.ManyToManyField(User, related_name="staff_teams")
team = models.CharField(max_length=50)
description = models.TextField(max_length=250)
verbose_name_plural = "Teams"
def __str__(self):
return self.team
serializers:::from apps.user.models.team import Team
from rest_framework import serializers
from apps.user.models.users import User
class TeamModelSerializer(serializers.ModelSerializer):
user = serializers.SlugRelatedField(queryset=User.objects.all(), many=True, slug_field= "username")
class Meta:
model = Team
fields = (
"id",
"user",
"team",
"description"
)
views::from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import mixins
from rest_framework.generics import GenericAPIView
from rest_framework.generics import RetrieveUpdateDestroyAPIView
from django.http import JsonResponse
from ..permissions import UserViewSetPermission
from apps.user.permissions import SuperAdminPermission
from drf_yasg.utils import swagger_auto_schema
from django.contrib.auth import get_user_model
from apps.user.models.team import Team
from ..serializers.team_serializers import TeamModelSerializer
User = get_user_model()
class TeamAPIView(APIView):
serializer_class = TeamModelSerializer
queryset = Team.objects.all()
permission_classes = [UserViewSetPermission]
@swagger_auto_schema(responses={200: TeamModelSerializer(many=True)})
def get(self, request):
teams = Team.objects.filter(user=request.user)
serializer = TeamModelSerializer(teams, many=True)
return Response(serializer.data)
class TeamCreateAPIView(APIView):
serializer_class = TeamModelSerializer
queryset = Team.objects.all()
permission_classes = [UserViewSetPermission]
@swagger_auto_schema(request_body=TeamModelSerializer)
def post(self, request):
serializer = TeamModelSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
class TeamDetailsAPIView(RetrieveUpdateDestroyAPIView):
serializer_class = TeamModelSerializer
queryset = Team.objects.all()
permission_classes = [UserViewSetPermission]
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dffc5785-a3ed-4839-9652-5cf05f411551n%40googlegroups.com.
No comments:
Post a Comment