如何在 Django 序列化器中只显示值?
How to display only values in Django Serializers?
我正在使用 'rest_serializer' 模块实现 Django REST API 框架:
当前输出为:
{
"count": 86,
"next": "http://127.0.0.1:8000/state/?page=2",
"previous": null,
"results": [
{
"state_name": "Alaska"
},
{
"state_name": "California"
},
...
]
}
如何将其显示为 json 列表:
[
"Alaska",
"California",
...
]
下面是我的序列化程序:
from .models import States
from rest_framework import serializers
class StateSerializer(serializers.ModelSerializer):
class Meta:
model = State
fields = ('state_name',)
view.py
class StateViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = States.objects.values('state_name').distinct();
serializer_class = StateSerializer
您可以覆盖 list
方法,由 listmodelmixin:
提供
from rest_framework.response import Response
class StateViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = States.objects.values('state_name').distinct();
serializer_class = StateSerializer
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
return Response(queryset.values_list('state_name', flat=True))
这是我要做的:因为你想要一个自定义的状态序列化表单,我会实现一个自定义序列化程序:
class RawStateSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return obj.state_name
之后就可以正常阅读了:
class StateViewSet(viewsets.ModelViewSet):
queryset = States.objects.values('state_name').distinct();
serializer_class = RawStateSerializer
注意它只支持读取(它将 return 只是一个字符串用于单个 GET
和一个字符串列表用于列表 GET
)。如果您还需要写入支持,则需要重写 .to_internal_value()
方法。
最后,如果您只需要用于列表组的特殊序列化程序,但其他操作需要常规序列化程序,您可以按照以下方式进行操作(基于我的 ):
class StateViewSet(viewsets.ModelViewSet):
queryset = States.objects.values('state_name').distinct();
def get_serializer_class(self):
if self.action == 'list':
return RawStateSerializer
return StateSerializer
我正在使用 'rest_serializer' 模块实现 Django REST API 框架:
当前输出为:
{
"count": 86,
"next": "http://127.0.0.1:8000/state/?page=2",
"previous": null,
"results": [
{
"state_name": "Alaska"
},
{
"state_name": "California"
},
...
]
}
如何将其显示为 json 列表:
[
"Alaska",
"California",
...
]
下面是我的序列化程序:
from .models import States
from rest_framework import serializers
class StateSerializer(serializers.ModelSerializer):
class Meta:
model = State
fields = ('state_name',)
view.py
class StateViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = States.objects.values('state_name').distinct();
serializer_class = StateSerializer
您可以覆盖 list
方法,由 listmodelmixin:
from rest_framework.response import Response
class StateViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = States.objects.values('state_name').distinct();
serializer_class = StateSerializer
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
return Response(queryset.values_list('state_name', flat=True))
这是我要做的:因为你想要一个自定义的状态序列化表单,我会实现一个自定义序列化程序:
class RawStateSerializer(serializers.BaseSerializer):
def to_representation(self, obj):
return obj.state_name
之后就可以正常阅读了:
class StateViewSet(viewsets.ModelViewSet):
queryset = States.objects.values('state_name').distinct();
serializer_class = RawStateSerializer
注意它只支持读取(它将 return 只是一个字符串用于单个 GET
和一个字符串列表用于列表 GET
)。如果您还需要写入支持,则需要重写 .to_internal_value()
方法。
最后,如果您只需要用于列表组的特殊序列化程序,但其他操作需要常规序列化程序,您可以按照以下方式进行操作(基于我的
class StateViewSet(viewsets.ModelViewSet):
queryset = States.objects.values('state_name').distinct();
def get_serializer_class(self):
if self.action == 'list':
return RawStateSerializer
return StateSerializer