django 在DRF的序列化器中添加链接到操作的url字段

slwdgvem  于 12个月前  发布在  Go
关注(0)|答案(4)|浏览(117)

我正在使用django rest框架。我想在视图的序列化器中包含一个定义在视图中的动作的url。
我的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

字符串
我的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)


我在ItemList视图中有一个名为comments的操作,它给出了特定项目的所有注解。我可以从URL /api/items/<slug>获取项目的详细信息。我可以从URL api/items/<slug>/comments获取项目的所有注解。我想在ItemDetailSerializer序列化程序中包含一个comments字段,该字段是到api/items/<slug>/commments的链接。我如何才能完成这?

vktxenjb

vktxenjb1#

你可以通过SerializerMethodFieldreverse来实现:

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

    ...

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

字符串

6bc51xsx

6bc51xsx2#

试试这个:

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)

字符串

ubbxdtey

ubbxdtey3#

You can use the HyperlinkedIdentityField for this:
comments = serializers.HyperlinkedIdentityField(
    view_name='item-comments',
    lookup_field='slug'
)
This will then render a comments field that contains a URL to the item-comments view, with the slug as lookup parameter.
You will need to register the view with the name item-comments, for example with:
urlpatterns = [
    path('items/<slug>/comments/', ItemList.as_view({'get': 'comments'}), name='item-comments')
]
Note that normally nested routes are discouraged, and you'd put the comments in a separate view. But the above should work.

字符串

kiz8lqtg

kiz8lqtg4#

router = DefaultRouter()
router.register('item', ItemList, basename='item')

urlpatterns = [
    path('', include(router.urls)),
]

comments = serializers.HyperlinkedIdentityField(view_name='item-comments',lookup_field='slug')

字符串

相关问题