Django 动态扩展基于 class 的视图
Django Extending class based view dynamically
在 Django 的基于 class 的视图中,是否有动态选择我想扩展哪个视图的好方法?
现在我有 2 个视图(更新和视图)如下,我想将其合并为 1 个:
from django.views.generic import UpdateView, DetailView
from guardian.mixins import PermissionRequiredMixin
class MyUpdateView(PermissionRequiredMixin, UpdateView):
model = MyModel
permission_required = 'MyApp.change_mymodel'
template_name = "MyApp/update.html"
fields = ["name", "Type", "description"]
class MyDetailView(PermissionRequiredMixin, DetailView):
model = MyModel
permission_required = 'MyApp.view_mymodel'
template_name = "MyApp/view.html"
我想做的是有1个视图,1个url,当访问视图时如果用户有更改权限,应该使用UpdateView,如果他们有查看权限使用DetailView,如果它们都没有显示某种错误。
有没有办法让它起作用?
编辑:最终解决方案:
class CombinedView(PermissionRequiredMixin, DetailView, UpdateView):
model = MyModel
fields = ["name", "Type", "description"]
def get_required_permissions(self, request=None):
if (self.request.user).has_perm('MyApp.change_mymodel', self.get_object()):
return ['MyApp.change_mymodel']
return ['MyApp.view_mymodel']
def get_template_names(self):
if (self.request.user).has_perm('MyApp.change_mymodel', self.get_object()):
return ["MyApp/update.html"]
return ["MyApp/view.html"]
重写 get_template_names()
方法和 get_required_permissions()
方法,
class CombinedView(PermissionRequiredMixin, DetailView, UpdateView):
model = MyModel
fields = ["name", "Type", "description"]
<b>def get_required_permissions(self, request=None): # alternate to `permission_required`
if self.request.method == 'POST':
return ['MyApp.change_mymodel']
return ['MyApp.view_mymodel']
def get_template_names(self): # alternate to `template_name`
if self.request.method == 'POST':
return ["MyApp/update.html"]
return ["MyApp/view.html"]</b>
在 Django 的基于 class 的视图中,是否有动态选择我想扩展哪个视图的好方法?
现在我有 2 个视图(更新和视图)如下,我想将其合并为 1 个:
from django.views.generic import UpdateView, DetailView
from guardian.mixins import PermissionRequiredMixin
class MyUpdateView(PermissionRequiredMixin, UpdateView):
model = MyModel
permission_required = 'MyApp.change_mymodel'
template_name = "MyApp/update.html"
fields = ["name", "Type", "description"]
class MyDetailView(PermissionRequiredMixin, DetailView):
model = MyModel
permission_required = 'MyApp.view_mymodel'
template_name = "MyApp/view.html"
我想做的是有1个视图,1个url,当访问视图时如果用户有更改权限,应该使用UpdateView,如果他们有查看权限使用DetailView,如果它们都没有显示某种错误。
有没有办法让它起作用?
编辑:最终解决方案:
class CombinedView(PermissionRequiredMixin, DetailView, UpdateView):
model = MyModel
fields = ["name", "Type", "description"]
def get_required_permissions(self, request=None):
if (self.request.user).has_perm('MyApp.change_mymodel', self.get_object()):
return ['MyApp.change_mymodel']
return ['MyApp.view_mymodel']
def get_template_names(self):
if (self.request.user).has_perm('MyApp.change_mymodel', self.get_object()):
return ["MyApp/update.html"]
return ["MyApp/view.html"]
重写 get_template_names()
方法和 get_required_permissions()
方法,
class CombinedView(PermissionRequiredMixin, DetailView, UpdateView):
model = MyModel
fields = ["name", "Type", "description"]
<b>def get_required_permissions(self, request=None): # alternate to `permission_required`
if self.request.method == 'POST':
return ['MyApp.change_mymodel']
return ['MyApp.view_mymodel']
def get_template_names(self): # alternate to `template_name`
if self.request.method == 'POST':
return ["MyApp/update.html"]
return ["MyApp/view.html"]</b>