How to override the default value of a Model Field from an Abstract Base Class

Viewed 9892

I've got some code that looks like this:

class BaseMessage(models.Model):
    is_public = models.BooleanField(default=False)
    # some more fields...

    class Meta:
        abstract = True

class Message(BaseMessage):
    # some fields...

and I'd like to override the default value of the is_public field in the Message model so that it's True for that model.

I've looked through some relevant Django docs and poked around the model objects but I'm having trouble finding the right place to do this. Any suggestions?

2 Answers

For validation level overload like max_length one could add this to apps.py ready method

from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _


class MyAppConfig(AppConfig):
    name = "myapp"
    verbose_name = _("myapp")

    def ready(self):
        from myapp.models import MyModel

        # HACK: to set max_length for field when you can't change
        # this on parent model for example when inheriting from third
        # party app
        MyModel._meta.get_field('field').max_length = 128
Related