# Path: hashid_field/rest.py
# class HashidSerializerIntegerField(HashidSerializerMixin, fields.IntegerField):
#     def to_representation(self, value):
#         return int(value)
# 
# class HashidSerializerCharField(HashidSerializerMixin, fields.CharField):
#     def to_representation(self, value):
#         return str(value)
# 
#     def to_internal_value(self, data):
#         hashid = super().to_internal_value(data)
#         if isinstance(data, int) and not self.allow_int_lookup:
#             self.fail('invalid_hashid', value=data)
#         if isinstance(data, str) and not self.allow_int_lookup:
#             # Make sure int lookups are not allowed, even if prefixed, unless the
#             # given value is actually a hashid made up entirely of numbers.
#             without_prefix = data[len(self.prefix):]
#             if _is_int_representation(without_prefix) and without_prefix != hashid.hashid:
#                 self.fail('invalid_hashid', value=data)
#         return hashid


from rest_framework import serializers
from hashid_field.rest import HashidSerializerIntegerField, HashidSerializerCharField
from library.models import Author, Book



class AuthorSerializer(serializers.HyperlinkedModelSerializer):
    id = HashidSerializerCharField(source_field='library.Author.id', read_only=True)

    class Meta:
        model = Author
        fields = ('url', 'id', 'name', 'uid', 'books')


class BookSerializer(serializers.HyperlinkedModelSerializer):
    reference_id = HashidSerializerCharField(salt="alternative salt")
    key = HashidSerializerIntegerField(source_field="library.Book.key")
    author_char = serializers.PrimaryKeyRelatedField(
        pk_field=HashidSerializerCharField(source_field='library.Author.id'),
        read_only=True, source='author'
    )
    author_int = serializers.PrimaryKeyRelatedField(
        pk_field=HashidSerializerIntegerField(source_field='library.Author.id'),
        read_only=True, source='author')
    author_string = serializers.StringRelatedField(source='author')
    author_slug = serializers.SlugRelatedField(slug_field='uid', read_only=True, source='author')
    editors = serializers.PrimaryKeyRelatedField(

# Path: hashid_field/rest.py
# class HashidSerializerIntegerField(HashidSerializerMixin, fields.IntegerField):
#     def to_representation(self, value):
#         return int(value)
# 
# class HashidSerializerCharField(HashidSerializerMixin, fields.CharField):
#     def to_representation(self, value):
#         return str(value)
# 
#     def to_internal_value(self, data):
#         hashid = super().to_internal_value(data)
#         if isinstance(data, int) and not self.allow_int_lookup:
#             self.fail('invalid_hashid', value=data)
#         if isinstance(data, str) and not self.allow_int_lookup:
#             # Make sure int lookups are not allowed, even if prefixed, unless the
#             # given value is actually a hashid made up entirely of numbers.
#             without_prefix = data[len(self.prefix):]
#             if _is_int_representation(without_prefix) and without_prefix != hashid.hashid:
#                 self.fail('invalid_hashid', value=data)
#         return hashid


# Path: sandbox/library/serializers.py
from rest_framework import serializers
from hashid_field.rest import HashidSerializerIntegerField, HashidSerializerCharField
from library.models import Author, Book




class AuthorSerializer(serializers.HyperlinkedModelSerializer):
    id = HashidSerializerCharField(source_field='library.Author.id', read_only=True)

    class Meta:
        model = Author
        fields = ('url', 'id', 'name', 'uid', 'books')


class BookSerializer(serializers.HyperlinkedModelSerializer):
    reference_id = HashidSerializerCharField(salt="alternative salt")
    key = HashidSerializerIntegerField(source_field="library.Book.key")
    author_char = serializers.PrimaryKeyRelatedField(
        pk_field=HashidSerializerCharField(source_field='library.Author.id'),
        read_only=True, source='author'
    )
    author_int = serializers.PrimaryKeyRelatedField(
        pk_field=HashidSerializerIntegerField(source_field='library.Author.id'),
        read_only=True, source='author')
    author_string = serializers.StringRelatedField(source='author')
    author_slug = serializers.SlugRelatedField(slug_field='uid', read_only=True, source='author')
    editors = serializers.PrimaryKeyRelatedField(

        pk_field=HashidSerializerCharField(source_field='library.Editor.id'),