英文:
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
字典中获取具有键 pk
的 MyDummy
对象。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论