serializers.py 973 B

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