Ask Your Question

Revision history [back]

In Django, we can specify the database in which a model (table) should be created by defining the database in the settings.py file. We can have multiple databases defined in the settings.py file, each with a unique name and set of configurations.

To specify the database for a particular model, we can use the Meta class within the model and set the db_table attribute to the name of the table we want to create in the specified database.

For example, if we want to create a table called mytable in a database named mydatabase, we can define a model as follows:

class MyModel(models.Model):
    # fields definition

    class Meta:
        db_table = 'mytable'
        app_label = 'myapp'
        managed = True
        using = 'mydatabase'

In the above code, the db_table attribute specifies the name of the table to be created in the database, using attribute specifies the name of the database to use, and app_label attribute specifies the name of the app containing this model. The managed attribute is set to True, which means Django should create the table for this model when running migrations.