使用Django一次更新几条记录

时间:2017-06-06 18:04:07

标签: python django database django-templates

我想知道如何通过一次点击从输入框中编辑一次更新数据的方法。我发现这篇关于如何使用复选框的帖子: Updating several records at once using Django  但是我不确定如何为输入框做这件事。

这是我的代码:

<table class="table table-striped table-bordered table-hover dataTables-example" >
    <thead>
          <tr>
              <th>Local</th>
              <th>line</th>
              <th>rack</th>
          </tr>
    </thead>
    <tbody>
        {% for linea in lineas_de_reporte %}
          <tr>
              <td>{{ linea.local }}</td>
              <td><input type="text" name="case-opened" value="{{ linea.rack }}" id="caseOpened"/></td>
              <td><input type="text" name="case-opened" value="{{ linea.line }}" id="caseOpened"/></td>

          </tr>
         {% endfor %}
     </tbody>

以下是它的外观: enter image description here

有没有办法只使用Django将所有值(按id)发送到列表中的函数?

编辑:

forms.py

class TVSForm(forms.Form):
    rack = forms.CharField()
    line = forms.CharField()

views.py

def search_folio(request):
if request.method == 'POST':

    form = FolioSearchForm(request.POST)
    if form.is_valid():
        folio_number = request.POST.get('folio_number', '')
        folio = 'FOLIO' + str(folio_number)
        folio_query = InventoryFolioManagement.objects.filter(folio=folio)
        context = {
            'lineas_de_reporte': folio_query,
            'folio': ' | ' + folio
        }
        return render(request, 'inv-search-folio.html', context)
else:
    form = FolioSearchForm()

if request.GET.get('rack'):
    # ... this means that user clicked on the submit button
    # get the id of linea
    linea_id = request.GET.get('rack-id')
    new_case = request.GET.get('rack')
    # fetch object from db, based on that id (aka pk)
    linea = TechnicalValidationSystem.objects.get(id=linea_id)
    # change its attribute to True
    linea.case_opened = new_case
    # update db with the new value
    linea.save(update_fields=['rack'])

return render(request, 'inv-search-folio.html')

0 个答案:

没有答案