检索没有模型的视图集的函数

huangapple go评论96阅读模式
英文:

Retrieve function for viewset without model

问题

I am using object without RDS entry.

class MyDummy(object):
    def __init__(self, **kwargs):
        for field in ('id', 'detail'):
            setattr(self, field, kwargs.get(field, None))

mydummys = {
    1: MyDummy(id=1, detail={"test":"test"}),
    2: MyDummy(id=2, detail={"test2":"test2"})
}

Serializer

class MyDummySerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    detail = serializers.JSONField(allow_null=True)
    
    def create(self, validated_data):
        return MyDummy(id=None, **validated_data)

    def update(self, instance, validated_data):
        for field, value in validated_data.items():
            setattr(instance, field, value)
        return instance

list function works well for now

class MyDummyViewSet(viewsets.ViewSet):
    serializer_class = s.MyDummySerializer
    def list(self, request):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=True)
        return Response(serializer.data)

However now I want to make retrieve for URL like api/mydummy/1

class MyDummyViewSet(viewsets.ViewSet):
    serializer_class = s.MyDummySerializer
    def list(self, request):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=True)
        return Response(serializer.data)
    def retrieve(self, request, pk=None):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=False)
        return Response(serializer.data)

It returns,

{
    "detail": null
}

I think I should narrow it down by id in retrieve. How can I make it?

英文:

I am using object without RDS entry.

Model

class MyDummy(object):
    def __init__(self, **kwargs):
        for field in ('id', 'detail'):
            setattr(self, field, kwargs.get(field, None))

mydummys = {
    1: MyDummy(id=1, detail={"test":"test"}),
    2: MyDummy(id=2,detail={"test2":"test2"})
}

Serializer

class MyDummySerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    detail = serializers.JSONField(allow_null=True)
    
    def create(self, validated_data):
        return MyDummy(id=None, **validated_data)

    def update(self, instance, validated_data):
        for field, value in validated_data.items():
            setattr(instance, field, value)
        return instance

list function works well for now

class MyDummyViewSet(viewsets.ViewSet):
    serializer_class = s.MyDummySerializer
    def list(self, request):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=True)
        return Response(serializer.data)

However now I want to make retreive for url like api/mydummy/1

class MyDummyViewSet(viewsets.ViewSet):
    serializer_class = s.MyDummySerializer
    def list(self, request):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=True)
        return Response(serializer.data)
    def retrieve(self, request, pk=None):
        serializer = s.MyDummySerializer(
        instance=m.mydummys.values(), many=False)
        return Response(serializer.data)

It returns,

{
    "detail": null
}

I think I should narrowing by id in retrieve.

How can I make it ?

答案1

得分: 1

你需要按照字典的键来过滤它,就像这样:

class MyDummyViewSet(viewsets.ViewSet):
    # ... 其他列表视图的部分 ...

    def retrieve(self, request, pk=None):
        instance = m.mydummys.get(int(pk))
        if instance is None:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = s.MyDummySerializer(instance=instance)
        return Response(serializer.data)

这将尝试从 mydummys 字典中获取具有键 pkMyDummy 对象。

英文:

You would need to filter it by the key of the dict like this:

class MyDummyViewSet(viewsets.ViewSet):
    # ... rest of your list view ...

    def retrieve(self, request, pk=None):
        instance = m.mydummys.get(int(pk))
        if instance is None:
            return Response(status=status.HTTP_404_NOT_FOUND)
        serializer = s.MyDummySerializer(instance=instance)
        return Response(serializer.data)

This would try to get MyDummy object that has a key pk in mydummys dict.

huangapple
  • 本文由 发表于 2023年7月4日 23:08:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定