Opened 4 years ago
Closed 4 years ago
#31847 closed New feature (wontfix)
Adding table prefix stripping to inspectdb
Reported by: | François Poulain | Owned by: | nobody |
---|---|---|---|
Component: | Core (Management commands) | Version: | 3.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I Plugged django on foreign db. It has tables prefixes. Stripping them from model name clarify the generated code and is welcome since the prefix isn't more meaningfull that the appname or the module name.
I did this as is:
--- a/base/management/commands/inspectdb.py +++ b/base/management/commands/inspectdb.py @@ -24,6 +24,10 @@ class Command(BaseCommand): parser.add_argument( '--include-partitions', action='store_true', help='Also output models for partition tables.', ) + parser.add_argument( + '--tables-prefix', nargs='?', type=str, default='', + help='Tables prefix to be stripped from model names.', + ) parser.add_argument( '--include-views', action='store_true', help='Also output models for database views.', ) @@ -40,7 +44,11 @@ class Command(BaseCommand): # 'table_name_filter' is a stealth option table_name_filter = options.get('table_name_filter') + tables_prefix = options['tables_prefix'] + def table2model(table_name): + if tables_prefix and table_name.startswith(tables_prefix): + table_name = table_name[len(tables_prefix):] return re.sub(r'[^a-zA-Z0-9]', '', table_name.title()) with connection.cursor() as cursor:
How do you think about it?
Note:
See TracTickets
for help on using tickets.
Thanks for the suggestion.
I don't think this is worth the complexity. You're meant to edit the models files
inspectdb
generates. It's just not worth us going down the path of adding various code paths to make small tweaks in the generation: better to do that in the editor.