• skip to sidebar
  • skip to search box

trainedmonkey

by Jim Winstead Jr.

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_")
» Monday, September 21 @ 4:46am » code, Python, Django » Bluesky butterfly logo » Comment
« Looking for a come up

Add a comment

Comments may be rejected or hidden if the robots decide that they look like spam.
  • Home
  • About
  • Archive
  • Bookmarks
  • Photos
  • Projects
  • GitHub
  • @jimw@mefi.social

Dedicated to the public domain by Jim Winstead Jr.