How to recover a field from a foreign key, to be used in another form without modifying the field to recover?

0

I have the following graphical interface

Where I want to unsubscribe a product, retrieving the code in the new "Submit Low" form.

This is my code

Code models.py

 class venta(models.Model):
     codigo = models.IntegerField()
     descripcion = models.CharField(max_length=100, unique=True)
     marca = models.CharField(max_length=40,blank=True)
     precio = models.DecimalField(max_digits=15, decimal_places=5, default=0)

     def __unicode__(self):
         return '{}'.format(self.codigo)

 class baja(models.Model):
     Venta = models.ForeignKey(venta)
     fecha_baja = models.DateField()
     observacion = models.CharField(max_length=200)
     estado = models.BooleanField(default=True)

Code forms.py

class baja_form(forms.ModelForm):
   class Meta:
      model = baja
      fields = ['Venta', 'fecha_baja', 'observacion','estado']
      labels = {}

def __init__(self, *args, **kwargs):
    super(baja_form, self).__init__(*args, **kwargs)
    for field in iter(self.fields):
        if field <> 'estado':
            self.fields[field].widget.attrs.update({
                'class': 'form-control'
            })

Code urls.py

url(r'^crear_bajas/$', views.crear_baja, name='crear_bajas'),

Code of the html document where the Submit Low button is

 <a onclick="return abrir_modal('{% url 'venta:crear_bajas' %}','Dar de Baja / Nuevo')" class="btn btn-warning"  type="button">Dar Baja</a>

Code where the Form of Losses is

 <form role="form" action="{% url 'venta:crear_bajas' %}" method="post">
   {% csrf_token %}
    <div class="panel panel-default">
     <div class="panel-body">
        {{ form.as_p }}
     </div>
    </div>
    <div class="row">
      <div class="col-lg-12 text-right">
         <input type="submit" class="btn btn-primary" name="submit" value="Guardar">
         <button type="button" class="btn btn-default" onclick="return cerrar_modal()">Cancelar</button>
      </div>
    </div>
  </form>

Until now the result is this

Instead of that select I would like to be able to recover the " code " of the product without the user modifying it, it's the only thing I can not do yet.

Code views.py of the table view

 def ventas_list(request):
     ventas_p = venta.objects.all().order_by('-id')
     contexto = {'ventas':ventas_p}
     return render(request, 'ventas/ventalist.html', contexto)
    
asked by Lun 26.07.2018 в 16:57
source

1 answer

1

first load the types of forms:

from django.views.generic.edit import CreateView, UpdateView, DeleteView

second for this case you are going to use

DeleteView

later in your view:

class baja(DeleteView): 
    model = "tu modelo"
    template_name = "tu template"
    def get_context_data(self, *args, **kwargs):

    "aca pones lo que queres eliminar"

"I HOPE TO BE HELPFUL"

    
answered by 27.07.2018 в 22:29