Use UpdateView for two Django operations

1

I want to use a view to do two operations. From a list of articles I want to add or subtract the current quantity of the article.

I duplicated my view and changed the name to reflect the operation I want to do, I created 2 URLs with different names for each one linked to the view I want to execute. Despite this I still have the same field at the time of doing the operation (only shows the field to subtract the amount).

class Items(models.Model):
    nombre = models.CharField(max_length=250)
    descripcion = models.CharField(max_length=250)
    codigo_proveedor = models.CharField(max_length=250)
    categoria = models.ForeignKey('Categorias', on_delete=models.CASCADE)
    c_minima = models.PositiveIntegerField()
    c_actual = models.PositiveIntegerField()
    c_descuento = models.PositiveIntegerField(blank=True)
    c_incremento = models.PositiveIntegerField(blank=True)
    proveedor = models.ForeignKey('Proveedores', on_delete=models.CASCADE)
    carrito = models.ForeignKey('Carrito', on_delete=models.CASCADE, null=True )
    p_unitario = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True )
    total = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
    material = models.ForeignKey(Materiales, null=True, blank=True)
    tipo = models.ForeignKey(Tipo, null=True, blank=True)
    active = models.BooleanField()

    def save(self, *args, **kwargs):
        self.c_actual = self.c_actual - self.c_descuento
        self.c_actual =self.c_actual + self.c_incremento 
        self.total = self.c_actual * self.p_unitario
        super(Items, self).save(*args, **kwargs)


    def __str__(self):
       return '%s %s %s %s' % (self.nombre, str(self.categoria), str(self.c_actual), str(self.total))

views.py

# DESCUENTA ARTICULOS DE LA LISTA DE ELEMENTOS 
class CatItemUpdateDesc(UpdateView):
    model = Items
    fields = ['c_descuento']
    template_name_suffix= '_update_form'
    context_object_name = 'items'
    success_url = reverse_lazy('inventory:cat-list')


    def get_context_data(self, **kwargs):
        context = super(CatItemUpdateDesc, self).get_context_data(**kwargs)
        context['categoria'] = Items.objects.all()
        return context

# INCREMENTA ARTICULOS EN LA LISTA 
class CatItemUpdateInc(UpdateView):
    model = Items
    fields = [ 'c_incremento']
    template_name_suffix= '_update_form'
    context_object_name = 'items'
    success_url = reverse_lazy('inventory:cat-list')


    def get_context_data(self, **kwargs):
        context = super(CatItemUpdateInc, self).get_context_data(**kwargs)
        context['categoria'] = Items.objects.all()
        return context

urls.py

#HACE EL DESCUENTO DE LAS CANTIDADES EXISTENTES
url(r'^item_update/(?P<pk>[-\w]+)/$', views.CatItemUpdateDesc.as_view(), name='CatItemUpdateDesc'),

#HACE EL DESCUENTO DE LAS CANTIDADES EXISTENTES
url(r'^item_update/(?P<pk>[-\w]+)/$', views.CatItemUpdateInc.as_view(), name='CatItemUpdateInc'),

Any suggestions on how I can achieve what I want?

    
asked by victorR 15.06.2018 в 21:19
source

1 answer

1

The problem is that you are using the same regular expression in both URLs:

r'^item_update/(?P<pk>[-\w]+)/$'

It seems to me that you can handle both cases in a single view using some kind of argument to help you decide whether to increase or decrease. If you are determined to use two different views (and therefore, two different URLs) then add something to differentiate them:

#HACE EL DESCUENTO DE LAS CANTIDADES EXISTENTES
url(r'^item_update/(?P<pk>[-\w]+)/desc/$', views.CatItemUpdateDesc.as_view(), name='CatItemUpdateDesc'),

#HACE EL DESCUENTO DE LAS CANTIDADES EXISTENTES
url(r'^item_update/(?P<pk>[-\w]+)/inc/$', views.CatItemUpdateInc.as_view(), name='CatItemUpdateInc'),

If you observe, I have added a desc/ and a inc/ respectively.

    
answered by 16.06.2018 / 01:42
source