Home > OS >  Adding a url field linking to an action in serializer in DRF
Adding a url field linking to an action in serializer in DRF

Time:10-23

I am using django rest framework. I want to include the url of an action defined in a view in its serializer.

My serializers.py:

from rest_framework import serializers
    

class CommentSerializer(serializers.ModelSerializer):
    """Serializer for comments."""
    
    class Meta:
        model = Comment
        fields = ["id", "item", "author", "content", "date_commented", "parent"]
    
    
class ItemDetailSerializer(serializers.ModelSerializer):
    """Serializer for items (for retrieving/detail purpose)."""
    
    category = CategorySerializer(many=True, read_only=True)
    media = MediaSerializer(many=True, read_only=True)
    brand = BrandSerializer(many=False, read_only=True)
    specifications = serializers.SerializerMethodField(source="get_specifications")
    comments = ??????????????????????????????????????????????????
    
    class Meta:
        model = Item
        fields = [
            "id",
            "slug",
            "name",
            "description",
            "brand",
            "show_price",
            "location",
            "specifications",
            "is_visible",
            "is_blocked",
            "created_at",
            "updated_at",
            "seller",
            "category",
            "media",
            "comments",
            "users_wishlist",
            "reported_by",
        ]
        read_only = True
        editable = False
        lookup_field = "slug"
    
    def get_specifications(self, obj):
        return ItemSpecificationSerializer(obj.item_specification.all(), many=True).data

My views.py:

from rest_framework import viewsets, mixins, status
from ramrobazar.inventory.models import Item, Comment
from ramrobazar.drf.serializers ItemSerializer, ItemDetailSerializer, CommentSerializer
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.filters import SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
    

class ItemList(viewsets.GenericViewSet, mixins.ListModelMixin):
    """View for listing and retrieving all items for sale."""
    
    queryset = Item.objects.all()
    serializer_class = ItemSerializer
    serializer_action_classes = {
        "retrieve": ItemDetailSerializer,
    }
    permission_classes = [IsAuthenticatedOrReadOnly]
    lookup_field = "slug"
    filter_backends = [DjangoFilterBackend, SearchFilter]
    filterset_fields = [
        "category__slug",
        "brand__name",
    ]
    search_fields = ["name", "description", "category__name", "brand__name", "location"]
    
    def get_serializer_class(self):
        try:
            return self.serializer_action_classes[self.action]
        except:
            return self.serializer_class
    
    def retrieve(self, request, slug=None):
        item = self.get_object()
        serializer = self.get_serializer(item)
        return Response(serializer.data)
        
    @action(detail=True, methods=['GET'])
    def comments(self, request, slug=None):
        item = Item.objects.get(slug=slug)
        queryset = Comment.objects.filter(item=item)
        serializer = CommentSerializer(queryset, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

I have an action named comments in the ItemList view which gives all the comments of a specific item. I can get the details of the item from the url /api/items/<slug>. I can get all the comments of the item from the url api/items/<slug>/comments. I want to include a comments field in the ItemDetailSerializer serializer which is a link to api/items/<slug>/commments. How can I accomplish this?

CodePudding user response:

You can do this via SerializerMethodField and reverse:

class ItemDetailSerializer(serializers.ModelSerializer):
    ...
    comments = serializers.SerializerMethodField(source="get_comments")

    ...

    def get_comments(self, obj):
        return reverse(YOUR_URL_NAME, kwargs={'slug': obj.slug})

CodePudding user response:

try this:

class ItemDetailSerializer(serializers.ModelSerializer):
...

    comments = serializers.CharField(max_length=100, required=False)

    def create(self, validated_data):
        validated_data['comments'] = self.context['request'].build_absolute_uri()   'comments'
        return super(ContentGalleryListSerializer, self).create(validated_data)
  • Related