Compact table names with Django RAPID
The last couple of Django applications that I’ve built have used the Django RAPID Architecture which I think fits better with how a larger web application grows and evolves. Part of it is putting all of the models into their own data app, but in doing so I like to avoid having the data_ prefix on every table. Putting this in app/data/__init__.py gets rid of that. This is basically the example from the manual except using removeprefix() (added in Python 3.9) instead of a regex.
from django.db.models.signals import class_prepared
from django.dispatch import receiver
@receiver(class_prepared)
def remove_table_prefix(sender, **kwargs):
sender._meta.db_table = sender._meta.db_table.removeprefix("data_")