text: import datetime as dt
  
  from rest_framework import serializers
  
  # import webcolors
  
  from .models import Cat, Owner, Achievement, AchievementCat, CHOICES
  
  
  class CatListSerializer(serializers.ModelSerializer):
      color = serializers.ChoiceField(choices=CHOICES)
  
      class Meta:
          model = Cat
          fields = ('id', 'name', 'color')
  
  
  class AchievementSerializer(serializers.ModelSerializer):
      achievement_name = serializers.CharField(source='name')
  
      class Meta:
          model = Achievement
          fields = ('id', 'name')
  
  
  class CatSerializer(serializers.ModelSerializer):
      achievements = AchievementSerializer(many=True, required=False)
      age = serializers.SerializerMethodField()
      # color = Hex2NameColor()
  
      class Meta:
          model = Cat
          fields = ('id', 'name', 'color', 'birth_year', 'owner', 'achievements',
                    'age')
  
      def get_age(self, obj):
          return dt.datetime.now().year - obj.birth_year
  
      def create(self, validated_data):
          if 'achievements' not in self.initial_data:
              cat = Cat.objects.create(**validated_data)
              return cat
          achievements = validated_data.pop('achievements')
          cat = Cat.objects.create(**validated_data)
          for achievement in achievements:
              current_achievement, status = Achievement.objects.get_or_create(
                  **achievement)
              AchievementCat.objects.create(
                  achievement=current_achievement,
                  cat=cat,
              )
          return cat
  
  
  class OwnerSerializer(serializers.ModelSerializer):
      cats = serializers.StringRelatedField(many=True, read_only=True)
  
      class Meta:
          model = Owner
          fields = ('first_name', 'last_name', 'cats')
  