Friday, January 29, 2021

multiplying two fields

Guys gd evening

I need help,really i don't kwnow how to do a multiply two fields, have a model called Articles.

class Articles(models.Model):
#models article
Nuevo = 'Nuevo'
Semi_nuevo = 'Semi_nuevo'
Usado = 'Usado'
Otros = 'Otros'
(Nuevo, 'Nuevo'),
name = models.CharField(max_length=50)
quantity = models.PositiveIntegerField()
fk_brand = models.ForeignKey(Brand, null=True, on_delete=models.SET_NULL)
model = models.CharField(max_length=50)
fk_category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
cost_buy = models.DecimalField(max_digits=10, decimal_places=2)
fk_supplier = models.ForeignKey(Supplier, null=True, on_delete=models.SET_NULL)
userful_life = models.DateField()
actual_state = models.CharField(max_length=12, choices=STATE_ACTUAL)
date_check = models.DateField()
location = models.CharField(max_length=50)
img = models.ImageField(upload_to='articles', null=True, blank=True)
description = models.TextField(blank=True)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)

class Meta:
verbose_name = 'Article'
verbose_name_plural = 'Articles'

def result(self,*args, **kwargs):
mult = self.cost_buy * self.quantity
return mult

class Meta:
verbose_name = 'Article'
verbose_name_plural = 'Articles'

def __str__(self):
return '{} {} {}'.format( ,self.cost_buy, self.quantity)

so i want to multiply 2 filed , cost_buy * quantity and the result show in the template

i was to try did this query in 
data = Articles.objects.all().annotate(result=F('coust_buy') * F('quantity')).output_field=FloatField('result')

but isn't work

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment