Django: how to change table names globally?

I know I can change table name per model using db_table attribute, as explained in the doc.

However, what if I want to do it globally? Let's say, I want all my table to be like:

db_table = f"table_{{model_name}}"

Is there any setting for this? Otherwise, I guess I could use a mixin to be inherited from. However, the whole purpose of doing this is to NOT think of table naming when any developer will add a new model.

After reading this issue, It seems maintainers didn't even consider to add this feature as they did not understand the need, which is pretty clear to me :/

Any idea? Thanks.

The default database table naming goes like db_table = f"{app_label}_{model_name}" and you can rewrite them As hedde-van-der-heide suggested, by creating an abstract base model and subclassing them on your models, like

from django.db import models


class BaseModel(models.Model):
    class Meta:
        abstract = True
        db_table = f"table_{model_name}"


class NewModel(BaseModel):
    name = models.CharField(max_length=128)

    def __str__(self):
        return str(self.name)
Back to Top