aldesabido
aldesabido

Reputation: 1288

DJango Migration Classes

Let's say I have an app called students and I have a multiple class(Class1, Class2,...) in my model.py. When I run the python manage.py migrate it will create a new table(students_class1, students_class2,...) in my database.

And now, my question is, is it possible to skip a specific class when migrating the app? like I don't want to create a table called students_class2

Thank you!

Upvotes: 0

Views: 76

Answers (1)

Ernest
Ernest

Reputation: 2949

You are probably looking for managed in the model's Meta class.

According to the docs:

If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal.

In case you already created migration, add 'managed': False to CreateModel's options:

from django.db.migrations import Migration as DjangoMigration, CreateModel


class Migration(DjangoMigration):
    operations = [
        CreateModel(
            name='ModelName',
            fields=[
                # Model's fields.
            ],
            options={
                # Other options.
                'managed': False
            }
        )
    ]

Also, consider using db_table for choosing an appropriate table name.

Upvotes: 2

Related Questions