Python 如何在 Django REST Framework 中返回自定义 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35019030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return custom JSON in Django REST Framework
提问by Roberto
I am trying to return custom json with get_queryset
but always get 404 error
in response.
我正在尝试返回自定义 json,get_queryset
但总是得到404 error
响应。
class TestViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Test.objects.all()
serializer_class = TestSerializer
def get_queryset(self):
if self.request.method == "GET":
content = {'user_count': '2'}
return HttpResponse(json.dumps(content), content_type='application/json')
If I delete everything starting from def
I'll got correct response with standard json data. What I am doing wrong?
如果我删除所有内容,def
我将得到标准 json 数据的正确响应。我做错了什么?
采纳答案by bakkal
If you don't need a ModelViewSet and just want custom JSON on a GET request
如果您不需要 ModelViewSet 并且只想在 GET 请求上使用自定义 JSON
You can also use an APIView
, which doesn't require a model
您也可以使用APIView
,它不需要模型
class MyOwnView(APIView):
def get(self, request):
return Response({'some': 'data'})
and
和
urlpatterns = [
url(r'^my-own-view/$', MyOwnView.as_view()),
]
With a ModelViewSet
使用 ModelViewSet
You've put the custom JSON into get_queryset, that's wrong. If you want to use a ModelViewSet
, this by itself should be enough:
您已将自定义 JSON 放入 get_queryset,这是错误的。如果你想使用 a ModelViewSet
,这本身就足够了:
class TestViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all()
serializer_class = TestSerializer
This ModelViewSet
comes with default implementations for .list()
, .retrieve()
, .create()
, .update()
, and .destroy()
. Which are available for you to override (customize) as needed
这种ModelViewSet
带有默认实现.list()
,.retrieve()
,.create()
,.update()
,和.destroy()
。您可以根据需要覆盖(自定义)哪些
Returning custom JSON from .retrieve()
and/or .list()
in ModelViewSet
从返回的定制JSON.retrieve()
和/或 .list()
在ModelViewSet
E.g. to override .retrieve()
to return custom view when retrieving a single object. We can have a look at the default implementation which looks like this:
例如,.retrieve()
在检索单个对象时覆盖以返回自定义视图。我们可以看看默认的实现,它看起来像这样:
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
So as an example to return custom JSON:
因此,作为返回自定义 JSON 的示例:
class TestViewSet(viewsets.ModelViewSet):
queryset = Test.objects.all()
serializer_class = TestSerializer
def retrieve(self, request, *args, **kwargs):
return Response({'something': 'my custom JSON'})
def list(self, request, *args, **kwargs):
return Response({'something': 'my custom JSON'})
回答by HoangYell
There are 2 ways to custom the response in Class-based viewswith ModelViewSet
有两种方法可以使用ModelViewSet在基于类的视图中自定义响应
Solution 1:custom in views.py
解决方案1:在views.py中自定义
class StoryViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.AllowAny,)
queryset = Story.objects.all()
serializer_class = StorySerializer
def retrieve(self, request, *args, **kwargs):
# ret = super(StoryViewSet, self).retrieve(request)
return Response({'key': 'single value'})
def list(self, request, *args, **kwargs):
# ret = super(StoryViewSet, self).list(request)
return Response({'key': 'list value'})
Solution 2:custom in serializers.py(I recommend this solution)
解决方案2:在serializers.py中自定义(我推荐这个解决方案)
class StorySerializer(serializers.ModelSerializer):
class Meta:
model = Story
fields = "__all__"
def to_representation(self, instance):
ret = super(StorySerializer, self).to_representation(instance)
# check the request is list view or detail view
is_list_view = isinstance(self.instance, list)
extra_ret = {'key': 'list value'} if is_list_view else {'key': 'single value'}
ret.update(extra_ret)
return ret