如何在Django中以可编辑形式检索保存复选框的值

时间:2019-08-11 08:15:08

标签: django django-models django-templates django-views

数据已保存到DB,在已启用的复选框中检索数据。如何在views.py file

中编写代码以编辑表单

在模型中,用户名字段是Emp_Profile模型的外键,而Process是Client_Process表的外键

Models.py

class Emp_Process(models.Model):
    username = models.ForeignKey(Emp_Profile, on_delete=models.CASCADE)
    process = models.ForeignKey(Client_Process, on_delete=models.CASCADE)
    class Meta:
        db_table : 'emp_process'

html文件

{% extends 'client_pannel.html'  %}
{% block content %}
<br><br>
<br><br>
<div class="data-table-area mg-b-15">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="sparkline13-list">
                    <div class="sparkline13-hd">
                        <div class="main-sparkline13-hd">
                                <label>{{ emp.first_name }} {{ emp.last_name }} - {{ emp.designation }}</label>
                        </div>
                    </div>
                    <div class="sparkline13-graph">
                        <div class="datatable-dashv1-list custom-datatable-overright">
                            <table id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" data-cookie="true"
                                data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar">
                                <thead>
                                    <tr>
                                        <th >Client</th>
                                        <th>Process</th>

                                    </tr>
                                </thead>
                                <tbody>
                                        {% for client in form %}
                                        <tr>
                                            <td>
                                                <input type="checkbox" name="cname[]" value="{{ client.id }}">
                                                {{ client.Name }}
                                            </td>
                                            <td>
                                                {% for process in client.clients %}
                                                <input type="checkbox" name="process[]" value="{{ process.id }}">
                                                {{ process.process }}<br />
                                                {% endfor %}
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

Views.py 在视图文件中,我通过ID获取员工详细信息

def Edit_Assigning(request, id):
form = Client.objects.all()
emp = Emp_Profile.objects.get(pk=id)
for i in range(len(form)):
    clients = Client.objects.filter(Name=form[i])
    object = Client.objects.filter(Name=form[i]).values('id')
    clients = Client_Process.objects.filter(client_id__in=object)
    form[i].clients = clients
return render(request, 'edit_assigning.html',{'emp': emp,'form':form})

0 个答案:

没有答案