CreateView如果模型中没有项目,我想显示存在的表单.否则我需要显示表格中存在的UpdateView.这样它就会加载已保存的值.稍后我应该通过调用update_or_create方法将数据保存到db .
这可能吗?
因此,下面是我在观看 Django Con EU 谈话视频后通过基于第一类的视图创建的。
它有效并且理解它的作用。我不明白基于类的视图和我刚刚构建的通用类基视图之间的区别?
class GroupListView(ListView):
"""
List all Groups.
"""
context_object_name = 'groups'
template_name = 'contacts/home.html'
def get_context_data(self, **kwargs):
"""
Get the context for this view.
"""
# Call the base implementation first to get a context.
context = super(GroupListView, self).get_context_data(**kwargs)
# Add more contexts.
context['tasks'] = Upload.objects.filter(uploaded_by=self.request.user).order_by('-date_uploaded')[:5]
context['unsorted'] = Contact.objects.unsorted_contacts(user=self.request.user).count()
return context
def get_queryset(self):
"""
Get the list of items for this view. This must be an iterable, and may
be a queryset (in which …Run Code Online (Sandbox Code Playgroud)