Skip to content

Instantly share code, notes, and snippets.

@RANUX
Created September 7, 2020 05:24
Show Gist options
  • Save RANUX/0056093acf770b47dbd1246a6aeb415a to your computer and use it in GitHub Desktop.
Save RANUX/0056093acf770b47dbd1246a6aeb415a to your computer and use it in GitHub Desktop.
Переименовать модель Django

Шаг 1: отредактируйте соответствующие имена полей в models.py

class Bar(models.Model):
    name = models.CharField(unique=True, max_length=32)
    description = models.TextField(null=True, blank=True)


class AnotherModel(models.Model):
    bar = models.ForeignKey(Bar)  # <-- changed field name
    is_awesome = models.BooleanField()


class YetAnotherModel(models.Model):
    bar = models.ForeignKey(Bar)  # <-- changed field name
    is_ridonkulous = models.BooleanField()

Шаг 2. Создайте пустую миграцию

python manage.py makemigrations --empty myapp

Шаг 3: Отредактируйте класс миграции в файле миграции, созданном на шаге 2

class Migration(migrations.Migration):

dependencies = [
    ('myapp', '0001_initial'), 
]

operations = [
    migrations.AlterField(
        model_name='AnotherModel',
        name='foo',
        field=models.IntegerField(),
    ),
    migrations.AlterField(
        model_name='YetAnotherModel',
        name='foo',
        field=models.IntegerField(),
    ),
    migrations.RenameModel('Foo', 'Bar'),
    migrations.AlterField(
        model_name='AnotherModel',
        name='foo',
        field=models.ForeignKey(to='myapp.Bar'),
    ),
    migrations.AlterField(
        model_name='YetAnotherModel',
        name='foo',
        field=models.ForeignKey(to='myapp.Bar'),
    ),
    migrations.RenameField('AnotherModel', 'foo', 'bar'),
    migrations.RenameField('YetAnotherModel', 'foo', 'bar')
]

Шаг 4: Примените миграцию

python manage.py migrate

Готово

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment