UpdateView problems updating a field

0

I am trying to make an inventory of tools, with which I can monitor the amount of tools I have, who has them (in which tool carts they are) and a record of the damaged tools.

I created a view where I display a list of existing carts or a button to create a new one. I can create new trolleys and update the trolleys

Apart from the update button there is a button to see the details of the cart

This view shows who is assigned to the cart and the tools that cart has, I can add tools to a cart. What I intend to do is to group all the tools and add the quantities per tool that exist and for each tool to have an update button to be able to unsubscribe.

Once in this view, I want to be able to add an update to each individual tool and disable it if I need to subtract it from the inventory.

My reasoning behind all this is the way I have my models for the carts, I have the models Item , Employees , Carritos and Transaction the carts table has a one-to-one ratio with employees since one cart can be assigned to one employee at a time, just as Carritos has a ratio of many many with Item through the table Transaction , this is keeping a record of each item that I add to the cart by the cart_id and tool_id so my table transaction has all the information I need, my problem is to moemnto quetar elements of the cart no matter which tool you click the update button is always the first item in the table that is wants to update.

I would appreciate any help and I hope it makes sense what I tell them to tell

my models.py for trolleys

# =========================================================================== #
#   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(default=1)
    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})

my views.py for the carts

# =========================================================================== #
#   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',
    ]


def detalle_carrito(request, pk):
    model = Carritos, Transaccion
    template_name = 'inventario/carrito/detalles_carrito.html'

    carritos = Carritos.objects.filter(pk=pk)
    # GEST ALL TOOLS ASSIGN TO CARRITO'S PK THAT ARE ACTIVE 
    # TRY TO GET ALL ACTIVE ITEMS THAT BELONG TO CARRITO = PK AND AGREGATE TOTAL ITEMS PER TYPE
    cantidades = Transaccion.objects.values('herramienta__description').annotate(Sum('cantidad')).filter(activo=True, carrito_id=pk)

    # GEST ALL TOOLS ASSIGN TO CARRITO'S PK THAT ARE NOT ACTIVE 
    eliminados = Transaccion.objects.filter(activo=False,carrito_id=pk)

    return render(request,template_name, {'carrito':carritos, 'trans':cantidades, 'eliminados':eliminados})



class CarritoUpdate(UpdateView):
    model = Carritos
    fields = [
        'no_carrito',
        'empleado',
        'activo',
    ]
    template_name_suffix = '_update_form' 




def detalle_Items(request, pk):
    model = Transaccion
    template_name = 'inventario/carrito/test-template.html'
    try:
        items_update = Transaccion.objects.filter(activo=True, carrito_id=pk, herramienta_id=pk)
    except Transaccion.DoesNotExist:
        raise Http404()

    return render(request, template_name, {'items_update':items_update})


class ItemUpdate(UpdateView):
    model = Transaccion
    fields = [
        'carrito',
        'herramienta',
        'cantidad',
        'tipo',
        'motivo',
        'activo',
    ]
    template_name_suffix = '_update_form' 

Basically what I'm doing is I'm keeping records of the tool I need in Transaction and I group with this query

cantidades = Transaccion.objects.values('herramienta__description').annotate(Sum('cantidad')).filter(activo=True, carrito_id=pk)

Let's say that if I want to put two tools of the same type in my table Transaction it would look something like this:

herramienta       cantidad 
+--------------+------------+
|  martillo    |      1     |
+--------------+------------+
|  martillo    |      1     |
+--------------+------------+
    
asked by victorR 26.07.2018 в 02:36
source

0 answers