from rest_framework import serializers from .models import TournamentModel class TournamentSerializer(serializers.ModelSerializer): levels = serializers.SerializerMethodField(read_only=True, required=False) level = serializers.ReadOnlyField() started = serializers.ReadOnlyField() finished = serializers.ReadOnlyField() class Meta: model = TournamentModel fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id"] def get_levels(self, instance): levels: [[int]] = [] for i in range(instance.level): level: [int] = instance.get_games_id_by_level(i) if (level == []): break levels.append(level) return levels