yamachan
yamachan

Reputation: 1079

Can I change DB table name from the default automatically on Django?

I would like to change the DB table names on Django1.9. I know we can change the standard issue with "db_table".

Class Foo_Bar:

    class Meta:
        db_table = "foo_bar"

But I need to write that in every single class and it is tiresome. I want Django creates a table whose name is lower-cased class name automatically. But I have no idea how to do.

I even tried to use

self.__class__.name__.lower()

but, I didn't know how Meta works well.

Any advice is helpful.

Thank you.

Upvotes: 1

Views: 387

Answers (1)

Selcuk
Selcuk

Reputation: 59184

A very hack-ish method but this code will give you the outer (since you want to get Foo_Bar, not Meta) class name:

import traceback

Class Foo_Bar:
    ...
    class Meta:
        db_table = traceback.extract_stack()[-2][2].lower()

Upvotes: 1

Related Questions