[Answer]-Dynamically alter django model's field return value

1👍

Override the __init__ method of your model field and set the max_digits based on the related unit of measure.

class ProductForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        self.fields['qty_available'] = models.DecimalField(max_digits=10, decimal_places=self.instance.uom.precision)
    class Meta:
        model = Product

0👍

If it’s just a presentation problem, you can use a simple_tag in your templates:

@register.simple_tag
def format_qty_available(product):
    return product.qty_available.quantize(Decimal(10) ** -product.uom.precision)

Leave a comment