class CommentListSerializer(serializers.ModelSerializer): reply_count = serializers.SerializerMethodField() replies = serializers.SerializerMethodField() user = UserDetailSeiralizer(read_only=True) url = serializers.HyperlinkedIdentityField( view_name='comments-api:detail', lookup_url_kwarg='comment_id' ) class Meta: model = Comment fields = [ 'id', 'object_id', 'parent', 'url', 'content', 'reply_count', 'replies', 'user', 'created_at', ] def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0
class CommentDetailSerializer(serializers.ModelSerializer): reply_count = serializers.SerializerMethodField() replies = serializers.SerializerMethodField() user = UserDetailSeiralizer(read_only=True) class Meta: model = Comment fields = [ 'id', 'content_type', 'object_id', 'content', 'reply_count', 'replies', 'user', 'created_at', ] read_only_fields = [ 'content_type', 'object_id', 'reply_count', 'replies', ] def get_replies(self, obj): if obj.is_parent: return CommentChildSerializer(obj.children(), many=True).data return None def get_reply_count(self, obj): if obj.is_parent: return obj.children().count() return 0
class StyleShareCreateUpdateSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) class Meta: model = StyleShare fields = [ 'image', 'items', 'tags', 'user', 'created_at', ]
class ItemListSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) class Meta: model = Item fields = [ 'item_code', 'item_name', 'image_url', 'price', 'user', 'created_at', ]
class StyleShareListSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) items = ItemDetailSerializer(read_only=True, many=True) tags = TagDetailSerializer(read_only=True, many=True) class Meta: model = StyleShare fields = [ 'id', 'image', 'items', 'tags', 'user', 'created_at', ]
class MagazineListSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) items = ItemDetailSerializer(read_only=True, many=True) class Meta: model = Magazine fields = [ 'id', 'bnr_image_url', 'main_image_url', 'title', 'content', 'user', 'items', 'created_at', ]
class PostListSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) url = serializers.HyperlinkedIdentityField(view_name='posts-api:detail', lookup_url_kwarg='post_id') class Meta: model = Post fields = [ 'id', 'url', 'category', 'title', 'content', 'user', 'created_at', ]
class CommentChildSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) replies = serializers.SerializerMethodField() class Meta: model = Comment fields = [ 'id', 'content', 'user', 'replies', 'created_at', ] # reply to field can be solution of nested comments # reply_to doesn't exists -> replies -> doesn't shown # reply_to exists -> replies -> should be shown def get_replies(self, obj): return CommentChildSerializer(obj.children(), many=True).data
class PostDetailSerializer(serializers.ModelSerializer): user = UserDetailSeiralizer(read_only=True) category = CategoryDetailSerializer(read_only=True) comments = serializers.SerializerMethodField() class Meta: model = Post fields = [ 'id', 'category', 'title', 'content', 'user', 'tags', 'created_at', 'comments', ] def get_comments(self, obj): c_qs = Comment.objects.filter_by_instance(obj) comments = CommentDetailSerializer(c_qs, many=True).data return comments