Django: delete a record from my table in one view and add the record in another table

0

I'm doing an inventory application for my workshop. I have a view that generates a tool cart, in the cart the employee assigned to the cart is assigned, the tools it contains and the quantity of each of them. What I intend to do is the following.

Maybe you can explain it better with an example. Suppose that the pepe cart has two 1/2 "cutters and one of them is damaged, I want to be able to unsuscribe it from the cart, save that transaction in a table.

Frankly I'm relatively new in programming and databases (although I've been learning for more than a year, it's only in my free time and when it's possible, heheh).

A how is my program at this time are two things that fail:

    • deactivating the tool deactivates the entire object (if the quantity 2 and only wanted to remove 1, everything is removed)
  • 2.- I can show a list of deactivated items but it is only the first object in the list

    models.py

    # =========================================================================== #
    #   MODELO PARA CREAR CARRITOS
    # =========================================================================== #
    
    
    class Carritos(models.Model):
        no_carrito = models.CharField(max_length=3, unique=True)
        empleado = models.OneToOneField(Empleados, on_delete=models.CASCADE)
        # empleado = models.ManyToManyField(Empleados,  through='Transaccion')
        items = models.ManyToManyField(Item, through='Transaccion', related_name='carritos')
        f_creacion = models.DateTimeField(auto_now_add=True)
        f_actualizacion = models.DateTimeField(auto_now=True)
        activo = models.BooleanField(default=True)
    
        def get_absolute_url(self):
            return reverse('inventario:carrito')#, kwargs={'pk': self.pk})
    
    
        class Meta:
            verbose_name_plural = "Carritos"
    
        def __str__(self):
            return self.no_carrito
    
    
    
    class Transaccion(models.Model):
    
        carrito = models.ForeignKey(Carritos, on_delete=models.CASCADE, related_name='items_carrito')
        herramienta = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='items_carrito')
        cantidad = models.PositiveSmallIntegerField()
        activo = models.BooleanField(default=True)
        tipo = models.CharField(max_length=10, choices=CONSUMIBLE, blank=True, null=True)
        motivo = models.CharField(max_length=10, blank=True, null=True)
    
    
        def get_absolute_url(self):
            return reverse('inventario:carrito')#, kwargs={'pk': self.pk})
    

    views.py

    # =========================================================================== #
    #   LOGICA PARA CREAR CARRITOS
    # =========================================================================== #
    # ===================> Logica relacinado con Cortadores <=====================#
    
    def home_carrito(request):
        template_name = 'inventario/carrito/createcarrito.html'
        model  = Carritos
    
        carritos = Carritos.objects.all()
        if carritos:
            return render(request, template_name, {'carritos':carritos})
        else:
            return render(request,template_name)
    
    
    class CarritoCreate(CreateView):
        model = Carritos
        fields = [
            'no_carrito',
            'empleado',
            'activo',
        ]
    
    class ItemCreate(CreateView):
        model = Transaccion
        fields = [
            'carrito',
            'herramienta',
            'cantidad',
        ]
    
    
    def detalle_carrito(request, pk):
        model = Carritos, Transaccion
        template_name = 'inventario/carrito/detalles_carrito.html'
    
        carritos = Carritos.objects.all().filter(pk=pk)
        trans =Transaccion.objects.filter(activo=True, carrito=pk)
        eliminados =Transaccion.objects.filter(activo=False, carrito=pk)
    
        return render(request,template_name, {'carrito':carritos, 'trans':trans, 'eliminados':eliminados} )
    
    
    
    class CarritoUpdate(UpdateView):
        model = Carritos
    
        fields = [
            'no_carrito',
            'empleado',
            'activo',
        ]
        template_name_suffix = '_update_form' 
    
    
    class ItemListUpdate(UpdateView):
        model = Transaccion
        fields = [
            'carrito',
            'herramienta',
            'cantidad',
            'tipo',
            'motivo',
            'activo',
    
    
        ]
    
        
    asked by victorR 20.07.2018 в 23:28
    source

    0 answers