Django.como subtract amount entered minus stock in product table?

0

I am new to python and it has been difficult for me to formulate a logic for this problem, it is necessary to subtract the amount entered from an article minus the stock that is already in the article table. here the classes:

class Pedido(models.Model):
    especialidad   = models.ForeignKey('Especialidad')
    fecha_entrega  = models.DateTimeField(auto_now_add=False)
    fecha_pedido   = models.DateTimeField(auto_now_add=True,null=True, blank=True)
    articulo       = models.ForeignKey('Articulo')    
    cantidad       = models.CharField(max_length=999, blank=True)
    pendiente      = models.CharField(max_length=999,  null=True, blank=True)


def __str__(self):
    return '{}'.format(self.articulo, self.cantidad) 

class Articulo(models.Model):
    cod_experto = models.CharField(max_length=999, primary_key=True, blank=True)
    nombre      = models.CharField(max_length=999, blank=True)
    descripcion = models.CharField(max_length=999, blank=True, null=True)
    info_bodega = models.ForeignKey(Bodega, null=True, blank=True, on_delete=models.CASCADE)
    stock       = models.CharField(max_length=999, blank=True)
    extmin      = models.CharField(max_length=999, blank=True, null=True)
    extmax      = models.CharField(max_length=999, blank=True, null=True)



def __str__(self):
    return '{}'.format(self.nombre) 

I would greatly appreciate the solution. Success! here the view (modified) at the tip of query set:

def succes(self):
    articulo   = Pedido.objects.filter(cod_experto=self.object.pk)
    for x in articulo:
        cant_articulo = Articulo.objects.filter(id=x.articulo_id)
        for z in cant_articulo:            
            total = x.cantidad - z.stock
            update = Articulo.objects.values('cantidad').filter(id=x.articulo_id).update(stock=total)
    return redirect(reverse('usuario:index.html', kwargs={'pk': self.object.pk}))
    return render(request, 'usuario:index.html' ,{'pk':pk})

I can not get an error to work:

Reverse for 'succes' with arguments '('',)' and keyword arguments '{}' not     found. 1 pattern(s) tried: ['Solicitudes/confirmar/(?P<articulo>\d+)/$']
url app: url(r'^confirmar/(?    P<articulo>\d+)/$',login_required(Pedidoapp.views.succes), name='succes'),
url global: url(r'^Solicitudes/', include(Pedidoapp.urls,     namespace="usuario")),

mark this as an error in the index.html:

Approve

    
asked by Demaro Create 09.02.2017 в 22:26
source

1 answer

1

The errors I see are the following. The one who is jumping right now, is this:

  

Reverse for 'successes' with arguments '(' ',)' and keyword arguments '{}' not found. 1 pattern (s) tried: ['Requests / confirm / (? P \ d +) / $']

What happens is that in your line return reverse('usuario:index.html', kwargs={'pk': self.object.pk}) you are sending in the parameter kwargs a key of 'pk', when the key that the wait has to be called articulo . That's what you do when you type ?P<articulo> in the url, or you can change this ?P<articulo> for this ?P<pk> . So you solve that error.

Second, if it's a view of django that function, from what I see in the urls, then it will return an error. Because you're not returning an object of type HttpResponse, if not a string, then it's best to do a return redirect(reverse('usuario:index.html', kwargs={'pk': self.object.pk})) if you see, I added the function redirect that comes from from django.shortcuts import redirect , so I would not drop that error.

Third, This will be the last point, because I do not want to get into your programming logic. But I see you have this:

...
articulo   = Pedido.objects.get(pk=1)
for x in articulo:
...

This would possibly throw an error, because, when doing a get() , it will return a single object, and when doing a for on that object, it would throw an error like that.

  

TypeError: 'Order' object is not iterable

Then you should change that line too.

Any questions, comments.

    
answered by 10.02.2017 в 15:57