【发布时间】:2023-03-25 09:29:01
【问题描述】:
有 3 个模型(公司、部门和员工组)显示为列表。
我正在尝试创建一个从 ListView 扩展的类基础视图,以通过在 URLpatterns 中传递模型名称来列出各种模型。
我为每个模型创建了单独的类视图,但本质上是复制粘贴代码。有没有办法将代码简化为一个类并从 urlpath 获取模型。
models.py
from django.db import models
...
class Company(models.Model):
id = models.CharField(max_length=5, primary_key=True)
name = models.CharField(max_length=100)
is_active = models.BooleanField()
...
class Division(models.Model):
id = models.CharField(max_length=5, primary_key=True)
name = models.CharField(max_length=100)
is_active = models.BooleanField()
...
class StaffGroup(models.Model):
name = models.CharField(max_length=20)
is_active = models.BooleanField()
...
urls.py
from django.urls import path
from srrp.views import srrpIndexView, srrpListView
app_name = 'srrp'
urlpatterns = [
path('', srrpIndexView.as_view(), name='srrp_index'),
path('<str:modelname>', srrpListView.as_view(), name='srrp_list'),
]
views.py
class srrpListView(ListView):
template_name = 'srrp/srrplist.html'
model = self.kwargs['modelname'] # I know this is wrong, this is just placeholder for the right solution
paginate_by = 10
【问题讨论】:
-
使用 model = None,然后在 dispatch 或其他早期方法中执行 model = self.kwargs['modelname']
标签: python-3.x django