serializers.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from rest_framework import serializers
  2. from projects.serializers.base_serializers import ProjectReferenceSerializer
  3. from .models import Team
  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 = ("dateCreated", "id", "isMember", "memberCount", "slug", "projects")
  20. def get_isMember(self, obj):
  21. user = self.context["request"].user
  22. return obj.members.filter(user=user).exists()
  23. def get_memberCount(self, obj):
  24. return obj.members.count()