serializers.py 925 B

1234567891011121314151617181920212223242526272829303132333435
  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. class Meta:
  6. model = Team
  7. fields = (
  8. "id",
  9. "slug",
  10. )
  11. class TeamSerializer(RelatedTeamSerializer):
  12. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  13. isMember = serializers.SerializerMethodField()
  14. memberCount = serializers.SerializerMethodField()
  15. class Meta(RelatedTeamSerializer.Meta):
  16. fields = (
  17. "dateCreated",
  18. "id",
  19. "isMember",
  20. "memberCount",
  21. "slug",
  22. )
  23. def get_isMember(self, obj):
  24. user = self.context["request"].user
  25. return obj.members.filter(user=user).exists()
  26. def get_memberCount(self, obj):
  27. return obj.members.count()