Reputation: 58928
Given an example app called x_y
with a model called FooBar
the generated table name is x_y_foobar
rather than the expected x_y_foo_bar
. This makes it harder to read and distinguish table names, especially with long names. Is there a global setting that I can use so I don't have to set each table name manually?
PS: I would be fine with model names like TLA
ending up as t_l_a
in the table name.
Upvotes: 2
Views: 154
Reputation: 107015
You can change the model's metaclass by subclassing ModelBase
:
import re
from django.db.models.base import ModelBase
class ModelMetaClass(ModelBase):
def __new__(cls, name, bases, attrs):
new_class = super(ModelMetaClass, cls).__new__(cls, name, bases, attrs)
new_class._meta.db_table = new_class._meta.app_label + '_' + '_'.join(map(str.lower, re.findall(r'[A-Z][^A-Z]*', name)))
return new_class
Then in your model:
class FooBar(models.Model):
__metaclass__ = ModelMetaClass
class Meta:
pass
Upvotes: 1