10

我有一个包含用户列表的页面,并且希望能够单击链接来更新他们的个人资料。单击“更新”时,我应该能够使用单个提交按钮在单个页面中编辑用户名、名字、...电子邮件、电话号码、部门等。我通过使用两种形式来实现这一点,一种用于用户,另一种用于额外信息。ListView、DeleteView 和 CreateView 与这两种形式完美配合,但不是 UpdateView。我无法使用初始数据实例化这两个表单。

问题是:如何用数据实例化这两个表单?覆盖self.object?get_form_kwargs?什么是最优雅的解决方案?

UpdateView 类如下。我不是在寻找“复制粘贴”解决方案,但也许可以为我指明正确的方向。

谢谢。

保罗

电话号码部门在名为 Employee 的模型中定义。

class Employee(models.Model):
    user = models.OneToOneField(User)
    phone_number = models.CharField(max_length=13, null=True)
    department = models.CharField(max_length=100)

模板是:

{% 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 %}

视图类是:

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')
4

2 回答 2

7

如果您需要的话,您应该能够使用“instance”kwarg 从现有模型中实例化表单。例子:

context['form'] = self.form_class(self.request.GET, instance=request.user)
于 2013-09-04T09:19:09.353 回答
1

尝试退出表单构造函数并使用“对象”变量self.request.getget_content_data这样get您就可以重新初始化构造函数,更多内容请参见此链接

我的代码:

 context['form'] = self.form_class(instance=self.object)
于 2017-12-14T16:14:46.120 回答