django rest框架在列表显示中隐藏特定字段? [英] django rest framework hide specific fields in list display?
问题描述
我想在
persons /
的列表显示中隐藏模型的特定字段,并显示所有详细信息上的字段显示persons / jane
I want to hide specific fields of a model on the list display at
persons/
and show all the fields on the detail displaypersons/jane
我对其余框架而言相对较新,文档感觉很难掌握。
I am relatively new to the rest framework and the documentation feels like so hard to grasp.
这就是我要完成的工作。
Here's what I am trying to accomplish.
我有一个简单的 Person
模型,
# model
class Person(models.Model):
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
nickname = models.CharField(max_length=20)
slug = models.SlugField()
address = models.TextField(max_length=300, blank=True)
和序列化程序类
# serializers
class PersonListSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ('nickname', 'slug')
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'nickname', 'slug', 'address')
和视图集。
# view sets (api.py)
class PersonListViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonListSerializer
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
$ b网址为 persons
的$ b
我要显示人员列表,仅包含字段昵称
和 slug
,并在网址 persons / [slug]
显示模型的所有字段。
at the url persons
I want to dispaly list of persons, just with fields nickname
and slug
and at the url persons/[slug]
I want to display all the fields of the model.
我的路由器配置,
router = routers.DefaultRouter()
router.register(r'persons', api.PersonListViewSet)
router.register(r'persons/{slug}', api.PersonViewSet)
我猜第二种配置是错误的,我该如何实现我想做的事情?
I guess the second configuration is wrong, How can I achieve what I am trying to do?
更新:
输出到 persons / slug
是 { detail:未找到。}
,但适用于 person / pk
the output to persons/slug
is {"detail":"Not found."}
but it works for person/pk
谢谢
推荐答案
对于遇到此问题的任何人,我发现在视图集上覆盖get_serializer_class并为每个操作定义一个序列化器是DRY-est选项(保留单个视图集,但允许动态序列化器选择):
For anyone else stumbling across this, I found overriding get_serializer_class on the viewset and defining a serializer per action was the DRY-est option (keeping a single viewset but allowing for dynamic serializer choice):
class MyViewset(viewsets.ModelViewSet):
serializer_class = serializers.ListSerializer
permission_classes = [permissions.IsAdminUser]
renderer_classes = (renderers.AdminRenderer,)
queryset = models.MyModel.objects.all().order_by('-updated')
def __init__(self, *args, **kwargs):
super(MyViewset, self).__init__(*args, **kwargs)
self.serializer_action_classes = {
'list':serializers.AdminListSerializer,
'create':serializers.AdminCreateSerializer,
'retrieve':serializers.AdminRetrieveSerializer,
'update':serializers.AdminUpdateSerializer,
'partial_update':serializers.AdminUpdateSerializer,
'destroy':serializers.AdminRetrieveSerializer,
}
def get_serializer_class(self, *args, **kwargs):
"""Instantiate the list of serializers per action from class attribute (must be defined)."""
kwargs['partial'] = True
try:
return self.serializer_action_classes[self.action]
except (KeyError, AttributeError):
return super(MyViewset, self).get_serializer_class()
希望这对其他人有帮助。
Hope this helps someone else.
这篇关于django rest框架在列表显示中隐藏特定字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!