Hej!
I need to do some simple calculations of filtered data in my django view. This works just fine when having values in the chosen filter. If the value is 0 or None I get an server error and the site collpases. Therefore I need a validator to make sure the given value is not 0 and not None.
(my attempt is at the bottom) Currently if the validator is added it gives me the message that ‘float’ object is not callable, even before getting to the filter option/template.
And: in my model is a m2m to the model where I get the values from. For each value can a unit be chosen and I only want to calculate if the units are identical, otherwise I want to get a warning.
Does anyone knows how to achieve that? Or where to look?
Any help is appreciated!
# validators.py
def validate_values(value):
if value == 0:
raise ValidationError(_('One of the given values is 0 or empty and the calculation therefore cannot be proceeded.'),
code='notinrange')
# views.py
def process_mass_intensity(request):
plants = Plant.objects.all()
myFilter = PlantsNameFilter(request.GET, queryset=plants)
plants = myFilter.qs
total_m = plants.aggregate(Sum('used_in_plant__value'))['used_in_plant__value__sum']
product_m = plants.aggregate(product_mass=Sum('used_in_plant__value',
filter=Q(used_in_plant__input_or_output='OUT')))['product_mass'](validators=[validate_values])
pmi = (total_m / product_m)(validators=[validate_values])
context = {"plants": plants, "myFilter": myFilter, "total_m": total_m, "product_m": product_m, "pmi": pmi}
return render(request, 'kpi/process_mass_intensity.html', context)
# models.py
class Plant(models.Model):
name = models.CharField(
max_length=200
)
used_xducts = models.ManyToManyField(
Xduct,
through="UsedXduct",
blank=True,
related_name="used_in_plant"
)
class UsedXduct(models.Model):
plant = models.ForeignKey(
Plant,
on_delete=models.PROTECT,
related_name="used_in_plant"
)
xduct = models.ForeignKey(
Xduct,
on_delete=models.PROTECT,
related_name="xduct"
)
value = models.FloatField()
quantity = models.ForeignKey(
Quantity,
on_delete=models.PROTECT
)
unit = models.ForeignKey(
Unit,
on_delete=models.PROTECT
)