Jedi Jedi - 1 year ago 68
SQL Question

Alias in Django ORM

I recently started working on a long-running MySQL-backed Django website. The code is mostly clean and well-written except for one patch in the database that seems to have been hurriedly applied.

Specifically, one of the Models was named

. As per the naming conventions applied to the rest of the website, this Model should be named

Unfortunately, there has already been too much built upon the wrong name, and it is infeasible to change the existing scripts (these include Django queryset operations as well as SQL statements). Replacing all instances of the wrong name is not possible, as not all code is owned by us; there are other users relying on this data.

Is there any way to alias
so that all future development uses the correct name? If yes, how will this impact the name of the underlying table? If not, what is the best way to handle something like this without introducing a performance hit (e.g. a "proxy table" or "proxy model" which is a one-to-one map)? As you can imagine, my team doesn't want to invest time in this non-issue (and probably justifiably) want me to ignore this.

Answer Source

A proxy model is not a one-to-one map; it has no representation in the database, and does exactly what you want.

Even simpler though is just to define an alias in your models; after the definition of tbl_Badge_data, you can just do:

Badge = tbl_Badge_data

and import Badge wherever you need it.