mysql - Django - 多选框 - 将更改保存到数据库

标签 mysql django

我能够获得适用于编辑模式的多选择框:

<section class="container">
<div>
    <select id="leftValues" size="5" multiple></select>
</div>
<div>
    <input type="button" id="btnLeft" value="&lt;&lt;" />
    <input type="button" id="btnRight" value="&gt;&gt;" />
</div>
<div>
    <select id="rightValues" size="4" multiple>

            {% for device in devices %} 
                <option>{{ device }}</option>
            {% endfor %} 
    </select>
</div>      
</section>      

    <div>
        <input type="text" id="txtRight" />
    </div>
 </section>

<button type="submit" id="save">Save Changes</button>
<!--button type="cancel">Cancel</button-->
<a href="{% url 'inventory:display_maintenance' %}" ><button type="button">Maintenance Page</button></a> 

</form>
<link rel="stylesheet" href="{% static 'css/msb.css' %}">
<script>
$("#btnLeft").click(function () {
    var selectedItem = $("#rightValues option:selected");
$("#leftValues").append(selectedItem);
});

$("#btnRight").click(function () {
    var selectedItem = $("#leftValues option:selected");
$("#rightValues").append(selectedItem);
});

$("#rightValues").change(function () {
    var selectedItem = $("#rightValues option:selected");
$("#txtRight").val(selectedItem.text());
});

但是当我选择“保存”按钮时,在对表进行更改后,再次在模板 edit_maintenance.html 中,它不会输入到数据库中。

views.py中与此相关的代码如下:

    def edit_maintenance(request, id):
        license_key = Maintenance.objects.get(id=id)
        maintenance_form = MaintenanceForm(instance=license_key) 
        devices = Devices.objects.filter(maintenance = id)

        if request.method == 'POST': 
           print(request.POST)

           maintenance_form = MaintenanceForm(request.POST, instance=license_key)

        if maintenance_form.is_valid(): 
           maintenance_form.save()
  #         return redirect('maintenance:edit_maintenance', id)

        args = { 
           'maintenance_form' : maintenance_form, 
           'devices' : devices 
            }

       return render(request, 'inventory/edit_maintenance.html', args) 
 

我对此还很陌生,所以任何提示、示例都会很棒。

最佳答案

可悲的是,这是新手的错误。我重新运行 makemigration 和 migrate,功能现在工作得很好......用于删除。现在我需要编写添加代码。

关于mysql - Django - 多选框 - 将更改保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59183413/

相关文章:

php - 在android中使用php从mysql数据库获取数据

python - Django 调试工具栏是否适用于 DRF?

Django-jquery : populate combobox based on selection of another combobox

django - 传递 Django Choices 表单字段的详细名称以查看上下文

python - 为django应用程序绘制类图

mysql - 如何显示mysql数据库中的隐藏表

Mysql存储过程错误——LOOP和CURSOR

python - 在 Django get_object_or_404 中选择特定字段

mysql - SQL 错误 MySQL 工作台

mysql - 数据仓库中的直方图