0

I was able to get a multiple select box working for edit mode:

<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());
});

But when I select the save button, after making changes to the table, again in the template, edit_maintenance.html, it does not feed into the database.

The code in the views.py that relates to this is as follows:

    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) 
 

I'm still fairly new to this so any tips, examples, would be great.

MarthaV
  • 3
  • 4
  • 1
    Welcome to SO. You should provide more details, especially the _logic_ (`views.py`) behind this template. – Pedram Parsian Dec 04 '19 at 19:31
  • Thank you @PedramParsian. ok, here's the code in the views.py – MarthaV Dec 05 '19 at 21:37
  • I believe I need to add more detail. I have used the code referenced in this stack overflow entry: https://stackoverflow.com/questions/15696415/html-multiple-select-box that was listed as an example http://jsfiddle.net/eUDRV/3/ the issue is, that I am not getting the changes that I make to the table reflected in the database. For example, if I move a device to the left hand side of the table I would like the foreign key relationship set to null; I'd like the relationship between device and maintenance removed while leaving device and maintenance tables in tact. – MarthaV Dec 10 '19 at 16:12

1 Answers1

0

sadly, this was a novice's mistake. I re-ran makemigration and migrate and the functionality works just fine now ... for delete. Now I need to work on the code for adding.

MarthaV
  • 3
  • 4