Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing DecimalField

I'm trying to create a DecimalField subclass, using the code below

class PositiveMoneyField(DecimalField):
    def __init__(self, verbose_name=None, name=None, **kwargs):
        super().__init__(self,
            verbose_name, name,
            max_digits=9, decimal_places=2,
            validators=[MinValueValidator(Decimal(0.0)), ],
            **kwargs
    )

But it give me this error:

TypeError: __init__() got multiple values for argument 'max_digits'

Any help is appreciated.

Thanks,

Eric

like image 339
Eric Acevedo Avatar asked Apr 01 '26 15:04

Eric Acevedo


1 Answers

After checking other django fields as SlugField, I found the solution:

class PositiveMoneyField(DecimalField):

    def __init__(self, *args, max_digits=9, decimal_places=2, **kwargs):
        kwargs.update({'validators': [MinValueValidator(Decimal(0.01)), ]})
        super().__init__(*args, max_digits=max_digits, decimal_places=decimal_places, **kwargs)
like image 65
Eric Acevedo Avatar answered Apr 04 '26 06:04

Eric Acevedo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!