Django REST框架在HTML视图中显示多个表单 [英] Django REST Framework show multiple forms on HTML view
问题描述
示例:
class UserViewSet(ViewSet):
model =用户
serializer_class = UserSerializer
@ decorators.detail_route(methods = ['put'])
def change_password (self,request,pk,* args,** kwargs):
serializer = UserChangePasswordSerializer(data = request.data)
...
@ decorators.detail_route = ['put'])
def update_prefs(self,request,* args,** kwargs):
serializer = UserPreferencesSerializer(data = request.data)
...
我有一切工作,我可以在User对象上执行标准的获取,发布,放置,删除操作两条额外的路线工作。但是,使用 BrowsableAPIRenderer
时,无法确定自定义路由的HTML表单是如何显示的。对于开发人员来说,能够看到上述put方法中预期的字段的格式非常方便,例如。
我尝试添加get方法两条路线,但这对于第一条路线是不感觉的。无论如何,当我去路由的URL时,我看不到正确的序列化程序,我看到在 serializer_class
中指定的序列号。
关于我没有尝试的唯一的事情是覆盖 get_serializer_class()
,因为这涉及到将每个路由器的每个序列化的知识放在一个地方,比在路由方法本身能够指定序列化器...这是我应该试图获得所需效果的方式吗?
事实证明,我需要不使用 serializer_class
而是覆盖 get_serializer_class
。由于这需要一个动作,我可以 if..elif..else
来返回正确的序列化程序。聪明地使用一个字典,这样就可以轻松实现:
serializers = {
'DEFAULT':UserSerializer,
'password_update':PasswordUpdateSerializer,
'register':UserRegisterSerializer,
}
def get_serializer_class(self,* args,** kwargs):
return self。 serializers.get(self.action,self.serializers ['DEFAULT'])
这将返回 UserSerializer
,除非我在序列化程序
字典中指定了一个不同的序列化程序。比我想象的更容易。
I have a ViewSet using Django REST Framework that has both standard and custom routes. The serializer for each route is different.
Example:
class UserViewSet(ViewSet):
model = User
serializer_class = UserSerializer
@decorators.detail_route(methods=['put'])
def change_password(self, request, pk, *args, **kwargs):
serializer = UserChangePasswordSerializer(data=request.data)
...
@decorators.detail_route(methods=['put'])
def update_prefs(self, request, *args, **kwargs):
serializer = UserPreferencesSerializer(data=request.data)
...
I have everything working such that I can perform standard get, post, put, delete actions on the User object and the two extra routes work. However, I can't figure out how to make the HTML forms for the custom routes display when using the BrowsableAPIRenderer
. It would be very handy for developers to be able to see forms demonstrating the fields that are expected on the above put methods, for example.
I tried adding get methods for the two routes, but this is non-sensical for the first one. Regardless, I don't see the right serializer displayed when I go to the URL for the route, I see the serializer specified in serializer_class
.
About the only thing I haven't tried is overriding get_serializer_class()
because this involves putting knowledge of every serializer for every route in a single place rather than being able to specify the serializer within the route method itself... is this the way I should be trying to get the desired effect?
It turns out I need to not use serializer_class
and instead override get_serializer_class
. Since this takes an action, I can if..elif..else
on that to return the right serializer. Clever use of a dictionary then makes this easy:
serializers = {
'DEFAULT': UserSerializer,
'password_update': PasswordUpdateSerializer,
'register': UserRegisterSerializer,
}
def get_serializer_class(self, *args, **kwargs):
return self.serializers.get(self.action, self.serializers['DEFAULT'])
This will return the UserSerializer
for all actions unless I specify a different serializer in the serializers
dictionary. Easier than I thought it would be.
这篇关于Django REST框架在HTML视图中显示多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!