我正在尝试创建一个Django
页面,其中可以更新某些内容,并且可以在分页表中查看某些内容。该模型如下所示:
class CostGroup(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
def get_absolute_url(self):
return reverse(
'costgroup_detail',
kwargs={
'costgroup_pk': self.pk,
}
)
class Cost(models.Model):
cost_group = models.ForeignKey(CostGroup)
amount = models.DecimalField(max_digits=50, decimal_places=2)
def get_absolute_url(self):
return reverse(
'cost_detail',
kwargs={
'cost_pk': self.pk,
}
)
因此,编辑表单适用于name
模型的description
和CostGroup
字段,表格应显示“金额”列表
我以前只使用表单模板中包含的表单和表UpdateView
来工作。现在,因为我想在表格中包含分页,我需要在同一页面上使用两个视图。我设计的页面最终应该看起来像这样:
我并不担心目前的风格,我目前的主要焦点是将表格和表格放在同一页面上。在目前的状态下,我唯一没有的就是桌子的分页:
目前的观点如下:
class CostDetail(UpdateView):
model = models.Cost
pk_url_kwarg = 'cost_pk'
template_name = 'main/cost_detail.html'
form_class = forms.CostDetailEditForm
success_url = reverse_lazy('cost_list')
我有一种感觉,利用Django CBV使用的基础混合物可能是要走的路,但我不知道如何开始。
非常感谢任何帮助
感谢您的时间
答案 0 :(得分:1)
(这个澄清似乎更好地作为新答案)
看起来你正在处理这两个表。对象级别使用CostGroup,而列表视图显示链接到CostGroup的成本中的子记录。假设这是真的,我将继续这样做:
class CostDetail(ModelFormMixin, ListView):
model = CostGroup # Using the model of the record to be updated
form_class = YourFormName # If this isn't declared, get_form_class() will
# generate a model form
ordering = ['id']
paginate_by = 10
template_name = 'main/cost_detail.html' # Must be declared
def get_queryset(self):
# Set the queryset to use the Cost objects that match the selected CostGroup
self.queryset = Cost.objects.filter(cost_group = get_object())
# Use super to add the ordering needed for pagination
return super(CostDetail,self).get_queryset()
# We want to override get_object to avoid using the redefined get_queryset above
def get_object(self,queryset=None):
queryset = CostGroup.objects.all()
return super(CostDetail,self).get_object(queryset))
# Include the setting of self.object in get()
def get(self, request, *args, **kwargs):
# from BaseUpdateView
self.object = self.get_object()
return super(CostDetail,self).get(request, *args, **kwargs)
# Include the contexts from both
def get_context_data(self, **kwargs):
context = ModelFormMixin.get_context_data(**kwargs)
context = ListView.get_context_data(**context)
return context
# This is the post method found in the Update View
def post(self, request, *args, **kwargs):
# From BaseUpdateView
self.object = self.get_object()
# From ProcessFormView
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
我没有尝试过这个,所以可能会有错误。祝你好运!
(请记住ccbv.co.uk是您在挖掘基于类的视图时的朋友)
答案 1 :(得分:0)
我正在开发的应用程序现在使用类似的方法。我从ListView开始,引入FormMixin,然后从FormView引入post()。
class LinkListView(FormMixin, ListView):
model = Link
ordering = ['-created_on']
paginate_by = 10
template_name = 'links/link_list.html'
form_class = OtherUserInputForm
#=============================================================================#
#
# Handle form input
#
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
self.form = form
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def put(self, *args, **kwargs):
return self.post(*args, **kwargs)
def get_success_url(self):
return reverse('links')
您可能还希望覆盖get_object()
,get_queryset()
和get_context()
。