Django: override field label or help_text in child model

I have several models that have common functionality. Each model is a physically populated type of element, so they share things like stock, and also share some things, such as low inventory warnings (sends an email).

Instead of duplicating the code, I wrote an abstract model and inherited it.

class LowStockModel(models.Model):
    stock = stock = models.IntegerField()
    out_of_stock_behaviour = models.CharField(max_length=20, choices=[...])

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        super(self.__class__, self).save(*args, **kwargs)
        if self.stock <= 0:
            #...

My problem is that I need to change the shortcut or add different ones help_textto the field stockin the child classes that I create. This is very important because the customer (and their staff) needs instructions for the devices. I tried to move around the child __init__, but I will not go anywhere.

tl; dr ?

+5
2

, Django :

CustomClass._meta.get_field('yourfield').verbose_name = 'Your custom text'

:

class AncestorClass(models.Model):
    stock = models.CharField(max_length=50, verbose_name='AncestorStockName')
    # etc ...

:

class ChildClass(AncestorClass):
     stock = models.CharField... # Customize your stock field

ChildClass._meta.get_field('stock').verbose_name = 'CustomVerboseStock'
+1

:

class MyForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['stock '].label = "any label"
0

All Articles