Django:如何使用UpdateView做两件不同的事情

时间:2018-06-14 22:24:36

标签: django django-templates django-views

我有一个项目列表,我想使用UpdateView来折扣或增加数量。我以不同的方式命名了我的2个视图,并为我的网址上的每个视图创建了2个链接,但不断获得相同的字段。

Models.py

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))

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'),

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

items list with button to select function to run

what i get if i click either of the buttons

0 个答案:

没有答案