'django - custom field from CharField with default args

I'm trying to use multiple custom fields in my project. For example CurrencyField. I want to give it a default arguments like verbose_name - "mena" etc.

Django raises error:

kwargs['verbose_name'] = kwargs['verbose_name'] or 'Mena'

I guess it's because I sometimes use verbose_name as a positional argument. How can I make it "universal"?

class CurrencyChoices(models.TextChoices):
    EUR = 'EUR'
    CHF = 'CHF'
    CZK = 'CZK'
    DKK = 'DKK'
    GBP = 'GBP'
    HRK = 'HRK'
    HUF = 'HUF'
    PLN = 'PLN'
    RON = 'RON'
    SEK = 'SEK'
    USD = 'USD'

class CurrencyField(models.CharField):

    def __init__(self, *args, **kwargs):
        kwargs['verbose_name'] = kwargs['verbose_name'] or 'Mena'
        kwargs['max_length'] = 5
        kwargs['choices'] = CurrencyChoices.choices
        super().__init__(*args, **kwargs)


Solution 1:[1]

If you do not specify the verbose_name in the constructor, it will raise a KeyError, you can work with .get(…) instead, or perhaps even better: .setdefault(…):

class CurrencyField(models.CharField):
    
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('verbose_name', 'Mena')
        kwargs['max_length'] = 5
        kwargs['choices'] = CurrencyChoices.choices
        super().__init__(*args, **kwargs)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem