serializers.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from rest_framework import serializers
  2. from .models import Team
  3. from projects.serializers.base_serializers import ProjectReferenceSerializer
  4. class RelatedTeamSerializer(serializers.ModelSerializer):
  5. """ Less detailed team serializer intended for nested relations """
  6. id = serializers.CharField(read_only=True)
  7. class Meta:
  8. model = Team
  9. fields = (
  10. "id",
  11. "slug",
  12. )
  13. class TeamSerializer(RelatedTeamSerializer):
  14. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  15. isMember = serializers.SerializerMethodField()
  16. memberCount = serializers.SerializerMethodField()
  17. projects = ProjectReferenceSerializer(many=True, read_only=True)
  18. class Meta(RelatedTeamSerializer.Meta):
  19. fields = (
  20. "dateCreated",
  21. "id",
  22. "isMember",
  23. "memberCount",
  24. "slug",
  25. "projects"
  26. )
  27. def get_isMember(self, obj):
  28. user = self.context["request"].user
  29. return obj.members.filter(user=user).exists()
  30. def get_memberCount(self, obj):
  31. return obj.members.count()