I have a form in which the following data is recorded:
ORIGEN
, CANTIDAD
, DEFECTO
, COMPONENTE
, COMENTARIO
and% DATATABLE
a ITEM
is selected. Once the information is captured, the record is saved.
The image below exemplifies the mentioned fields.
To be able to edit a certain record, I can now recover the following fields:
ORIGEN, CANTIDAD, DEFECTO, CANTIDAD, COMPONENTE, COMENTARIO
however, I do not know how to recover ITEM
in DATATABLE
to select a new one.
Function code.
def EditarItemValeFgView(request,id_parte):
editDetalle=DetalleParte.objects.get(parte_id=id_parte)
piezas=Parte.objects.all() //Se obtiene todas las piezas o ITEMS que se mostraran el DATATABLES
if request.method=='POST':
form=DetalleParteForm(request.POST)
if form.is_valid():
editDetalle.origen=form.cleaned_data.get('origen')
editDetalle.defecto=form.cleaned_data.get('defecto')
editDetalle.parte=parte_object
editDetalle.cantidad=form.cleaned_data.get('cantidad')
editDetalle.precioUnitarioPieza=parte_object.precioUnitario
editDetalle.comentario=form.cleaned_data.get('comentario')
editDetalle.subTotal=CalcularSubtotal(p.cantidad,parte_object.precioUnitario)
editDetalle.save()
if request.method=='GET':
form = DetalleParteForm(initial={
'origen':editDetalle.origen, //Se obtiene el origen
'defecto':editDetalle.defecto, //Se obtiene defecto
'cantidad':editDetalle.cantidad, //Se obtiene la cantidad
'componente':editDetalle.componente, //Se obtiene componente
'comentario':editDetalle.comentario}) //Se obtiene comentario
contex={'form':form,'piezas':piezas} //Se retorna formulario y pieza
return render(request,'app/editarpiezafg.html',contex)
Template code.
{% extends "app/layout.html" %}
{% load filters %} //Sirve para desplegar las opciones en las listas desplegables
{% block content %}
<br>
<center><h2>Agregar pieza MAC FG</h2></center>
<br/>
<form class="form-horizontal" method="post" id="agregar-piezafg-form">
{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col-xs-2">
<label class="col-xs-12 control-label">Origen:</label>
</div>
<div class="col-xs-4">
{{form.origen}}
<div class="text-danger">{{form.origen.errors}}</div>
</div>
<div class="col-xs-2">
<label class="col-xs-12 control-label">Defecto:</label>
</div>
<div class="col-xs-4">
{{form.defecto}}
<div class="text-danger">{{form.defecto.errors}}</div>
</div>
</div>
<br />
<div class="row">
<div class="col-xs-2">
<label class="col-xs-12 control-label">Cantidad:</label>
</div>
<div class="col-xs-4">
{{form.cantidad}}
<div class="text-danger">{{form.cantidad.errors}}</div>
</div>
<div class="col-xs-2">
<label class="col-xs-12 control-label">Componente:</label>
</div>
<div class="col-xs-4">
{{form.componente}}
<div class="text-danger">{{form.componente.errors}}</div>
</div>
</div>
<br />
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-4">
<br />
<br />
<div class="alert alert-success" role="alert">
Último registro<br />
-------------------------<br />
NP: {{utm}} <br />
Cantidad: {{utm.cantidad}}<br/>
Origen:{{utm.origen|origenFilter}}<br/>
Defecto: {{utm.defecto|defectosValeFgFilter}}
</div>
</div>
<div class="col-sm-2">
<label class="col-xs-12 control-label">Comentario:</label>
</div>
<div class="col-sm-4">
{{form.comentario}}
<div class="text-danger">{{form.comentario.errors}}</div>
</div>
<br/>
</div>
</div>
<br/>
<div class="container">
<br>
<br>
{% if piezas %}
<table id="datatables" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Seleccionar</th>
<th>Unidad Negocio</th>
<th>Proyecto</th>
<th>Numero de parte</th>
<th>Modelo</th>
<th>$ Unitario</th>
<th>Nivel</th>
</tr>
</thead>
<tbody>
{% for item in piezas %}
<tr>
//Sirve para activar el boton guardar, siempre y cuando se haya seleccionado un radio buttom
<td>
<label class="btn btn-success active">
<input autocomplete="off" name="checked_parte" type="radio" id="checked_parte" value="{{item.id}}" onChange="comprobar(this);"/>
</label>
</td>
<td>{{item.unidadNegocio}}<br></td>
<td>{{item.proyecto}}<br></td>
<td>{{item.numeroParte}}<br></td>
<td>{{item.modelo}}<br></td>
<td>${{item.precioUnitario}}<br></td>
<td>{{item.nivel}}<br></td>
</tr>
{% endfor %}
{% else %}
{% endif %}
</tbody>
</table>
<br>
<div class="col-sm-9">
</div>
<div class="col-sm-3">
<button name="boton" type="submit" id="boton" class="btn btn-success" disabled>Guardar</button>
<a href="/finPieza/{{v}}/" class="btn btn-danger">Finalizar</a> //Este botòn sirve para calcular el subtotal y total de cada una de las piezas seleccionadas
</div>
<br>
<br>
</div>
</form>
{% endblock %}