我正在Django 1.3中实现基于类的视图,我发现自己在这个场景中我的CreateView,UpdateView和DeleteView几乎完全相同。有没有办法用一个视图CreateUpdateView或类似的东西来实现它,或者这是实现CBGV的标准方法?
另外,在ThingyAdd中我没有像ThingyEdit那样指定模型,但它们都运行良好。我假设模型是由form_class,ThingyForm的meta部分中定义的模型暗示/拾取的,它是一个ModelForm。这个假设是否正确?
class ThingyAdd(AuthMixin, CreateView):
form_class = ThingyForm
context_object_name='object'
template_name='change_form.html'
success_url='/done/'
class ThingyEdit(AuthMixin, UpdateView):
model = Thingy
form_class = ThingyForm
context_object_name='object'
template_name='change_form.html'
success_url='/done/'
class ThingyDelete(AuthMixin, DeleteView):
model = Thingy
form_class = ThingyForm
context_object_name='object'
template_name='delete_confirmation.html'
success_url='/done/'
答案 0 :(得分:2)
你可以创建另一个mixin
class ThingyMixin(object):
model=Thingy
form_class=ThingyForm
template_name='change_form.html'
context_object_name='object'
success_url='/done/'
然后在你的观点中:
class ThingyAdd( AuthMixin, ThingyMixin, CreateView ):
pass
class ThingyEdit( AuthMixin, ThingyMixin, UpdateView ):
pass
class ThingyDelete( AuthMixin, ThingyMixin, DeleteView ):
template_name='delete_confirmation.html'