from rest_framework import serializers

from base.serializer import UserSerializerMixin
from .models import Market, Category


class MarketSerializer(UserSerializerMixin):
    category = serializers.SerializerMethodField()

    @staticmethod
    def get_category(obj):
        return obj.get_categories

    class Meta:
        model = Market
        fields = ('id', 'name', 'categories', 'user', 'category')
        extra_kwargs = {
            'categories': {'write_only': True},
        }


class CategoriesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('id', 'name', 'slug')
        read_only_fields = ('slug',)
