1234567891011121314151617181920212223242526272829303132333435 |
- from rest_framework import serializers
- from .models import Team
- class RelatedTeamSerializer(serializers.ModelSerializer):
- """ Less detailed team serializer intended for nested relations """
- class Meta:
- model = Team
- fields = (
- "id",
- "slug",
- )
- class TeamSerializer(RelatedTeamSerializer):
- dateCreated = serializers.DateTimeField(source="created", read_only=True)
- isMember = serializers.SerializerMethodField()
- memberCount = serializers.SerializerMethodField()
- class Meta(RelatedTeamSerializer.Meta):
- fields = (
- "dateCreated",
- "id",
- "isMember",
- "memberCount",
- "slug",
- )
- def get_isMember(self, obj):
- user = self.context["request"].user
- return obj.members.filter(user=user).exists()
- def get_memberCount(self, obj):
- return obj.members.count()
|