基于 Django 类的视图 - 具有两种模型表单的 UpdateView - 一种提交 [英] Django class based views - UpdateView with two model forms - one submit
问题描述
我有一个包含用户列表的页面,我希望能够点击链接来更新他们的个人资料.单击更新"后,我应该能够使用单个提交按钮在单个页面中编辑用户名、名字、...电子邮件、电话号码、部门等.我通过使用两种形式来实现这一点,一种用于用户,一种用于额外信息.ListView、DeleteView 和 CreateView 与这两种形式完美兼容,但不适用于 UpdateView.我无法使用初始数据实例化这两个表单.
问题是:如何用数据实例化这两个表单?覆盖self.object?get_form_kwargs?最优雅的解决方案是什么?
UpdateView 类如下.我不是在寻找复制粘贴"解决方案,但可能会为我指明正确的方向.
谢谢.
保罗
电话号码、部门在名为 Employee 的模型中定义.
class Employee(models.Model):用户 = 模型.OneToOneField(用户)phone_number = models.CharField(max_length=13, null=True)部门=models.CharField(max_length=100)
模板为:
{% 扩展 "baseadmin.html" %}{% 加载crispy_forms_tags %}{% 块内容 %}<h4>编辑用户</h4><form action="" method="post" class="form-horizontal"><legend>编辑用户</legend>{% 酥脆形式 %}{%脆皮形式2%}<div class="form-actions"><input type="submit" class="btn btn-primary" value="保存"><a href="{% url 'client_list' %}" class="btn">取消</a>
</表单>{% 端块含量 %}
视图类是:
class ClientUpdateView(UpdateView):模型 = 用户form_class = ClientsUserFormsecond_form_class = ClientsForm模板名称 = 'admin/client_update.html'def get_context_data(self, **kwargs):context = super(ClientUpdateView, self).get_context_data(**kwargs)上下文['active_client'] = True如果形式"不在上下文中:context['form'] = self.form_class(self.request.GET)如果form2"不在上下文中:上下文['form2'] = self.second_form_class(self.request.GET)上下文['active_client'] = True返回上下文def get(self, request, *args, **kwargs):super(ClientUpdateView, self).get(request, *args, **kwargs)form = self.form_classform2 = self.second_form_class返回 self.render_to_response(self.get_context_data(object=self.object,form=form,form2=form2))def post(self, request, *args, **kwargs):self.object = self.get_object()form = self.form_class(request.POST)form2 = self.second_form_class(request.POST)如果 form.is_valid() 和 form2.is_valid():userdata = form.save(commit=False)# 用于设置密码,但不再需要用户数据.save()员工数据 = form2.save(commit=False)员工数据.用户 = 用户数据员工数据.save()messages.success(self.request, '设置保存成功')返回 HttpResponseRedirect(self.get_success_url())别的:返回 self.render_to_response(self.get_context_data(form=form, form2=form2))def get_success_url(self):返回反向('client_list')
如果您需要,您应该能够使用实例"kwarg 从现有模型实例化表单.示例:
context['form'] = self.form_class(self.request.GET, instance=request.user)
I have a page with a list of users, and would like to be able to click a link to update their profile. When 'update' is clicked, I should be able to edit the username, first name, ... email, phone number, department etc, in a single page, using a single submit button. I accomplished this by using two forms, one for User, and one for the extra information. The ListView, DeleteView and CreateView work perfectly with these two forms, but not the UpdateView. I am not able to instantiate the two forms with initial data.
The question is: how do I instantiate the two forms with data? Overwrite self.object? get_form_kwargs? What would be the most elegant solution?
The UpdateView class is below. I am not looking for a 'copy-paste' solution, but maybe point me into the right direction.
Thanks.
Paul
The phone number, department is defined in a model named Employee.
class Employee(models.Model):
user = models.OneToOneField(User)
phone_number = models.CharField(max_length=13, null=True)
department = models.CharField(max_length=100)
The template is:
{% extends "baseadmin.html" %}
{% load crispy_forms_tags %}
{% block content %}
<h4>Edit a user</h4>
<form action="" method="post" class="form-horizontal">
<legend>Edit a user</legend>
{% crispy form %}
{% crispy form2 %}
<div class="form-actions">
<input type="submit" class="btn btn-primary" value="Save">
<a href="{% url 'client_list' %}" class="btn">Cancel</a>
</div>
</form>
{% endblock content %}
The view class is:
class ClientUpdateView(UpdateView):
model = User
form_class = ClientsUserForm
second_form_class = ClientsForm
template_name = 'admin/client_update.html'
def get_context_data(self, **kwargs):
context = super(ClientUpdateView, self).get_context_data(**kwargs)
context['active_client'] = True
if 'form' not in context:
context['form'] = self.form_class(self.request.GET)
if 'form2' not in context:
context['form2'] = self.second_form_class(self.request.GET)
context['active_client'] = True
return context
def get(self, request, *args, **kwargs):
super(ClientUpdateView, self).get(request, *args, **kwargs)
form = self.form_class
form2 = self.second_form_class
return self.render_to_response(self.get_context_data(
object=self.object, form=form, form2=form2))
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.form_class(request.POST)
form2 = self.second_form_class(request.POST)
if form.is_valid() and form2.is_valid():
userdata = form.save(commit=False)
# used to set the password, but no longer necesarry
userdata.save()
employeedata = form2.save(commit=False)
employeedata.user = userdata
employeedata.save()
messages.success(self.request, 'Settings saved successfully')
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(
self.get_context_data(form=form, form2=form2))
def get_success_url(self):
return reverse('client_list')
You should be able to use the "instance" kwarg for the instantiation of the form from an existing model if that's what you need. Example:
context['form'] = self.form_class(self.request.GET, instance=request.user)
这篇关于基于 Django 类的视图 - 具有两种模型表单的 UpdateView - 一种提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!