Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the database table name of a Model instance?

Tags:

django

Given a model's instance object, how can I get the database table's name?

I don't want to specify names explicitly in the Meta class.

like image 523
Ber Avatar asked Oct 24 '08 10:10

Ber


People also ask

How do you get a model table name?

You can get table name simply call getTable() of model object, so how to call this method as bellow example. $item = new Item; $table = $item->getTable(); print_r($table);

How to define table name in model in Django?

A model's database table name is constructed by joining the model's “app label” – the name you used in manage.py startapp – to the model's class name, with an underscore between them.

What are model instances?

The instance model is made up of Instances and links that are based on the class model. An Instance is an instance of a Class, Data Type, Interface or Signal that that shows features (Attributes and Roles) of that item through Slots. Each Slot corresponds to a single feature, and a Slot may include a value.


1 Answers

Found the answer myself: the _meta attribute of an instance has the information:

model_instance._meta.db_table 
like image 189
Ber Avatar answered Sep 22 '22 12:09

Ber