當前位置: 首頁>>代碼示例>>Python>>正文


Python migrations.RenameField方法代碼示例

本文整理匯總了Python中django.db.migrations.RenameField方法的典型用法代碼示例。如果您正苦於以下問題:Python migrations.RenameField方法的具體用法?Python migrations.RenameField怎麽用?Python migrations.RenameField使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.db.migrations的用法示例。


在下文中一共展示了migrations.RenameField方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_alter_field_rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_alter_field_rename_field(self):
        """
        RenameField should optimize to the other side of AlterField,
        and into itself.
        """
        self.assertOptimizesTo(
            [
                migrations.AlterField("Foo", "name", models.CharField(max_length=255)),
                migrations.RenameField("Foo", "name", "title"),
                migrations.RenameField("Foo", "title", "nom"),
            ],
            [
                migrations.RenameField("Foo", "name", "nom"),
                migrations.AlterField("Foo", "nom", models.CharField(max_length=255)),
            ],
        ) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:18,代碼來源:test_optimizer.py

示例2: test_optimize_through_fields

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_optimize_through_fields(self):
        """
        field-level through checking is working. This should manage to collapse
        model Foo to nonexistence, and model Bar to a single IntegerField
        called "width".
        """
        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]),
                migrations.CreateModel("Bar", [("size", models.IntegerField())]),
                migrations.AddField("Foo", "age", models.IntegerField()),
                migrations.AddField("Bar", "width", models.IntegerField()),
                migrations.AlterField("Foo", "age", models.IntegerField()),
                migrations.RenameField("Bar", "size", "dimensions"),
                migrations.RemoveField("Foo", "age"),
                migrations.RenameModel("Foo", "Phou"),
                migrations.RemoveField("Bar", "dimensions"),
                migrations.RenameModel("Phou", "Fou"),
                migrations.DeleteModel("Fou"),
            ],
            [
                migrations.CreateModel("Bar", [("width", models.IntegerField())]),
            ],
        ) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:26,代碼來源:test_optimizer.py

示例3: test_rename_field_reloads_state_on_fk_target_changes

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_rename_field_reloads_state_on_fk_target_changes(self):
        """
        If RenameField doesn't reload state appropriately, the AlterField
        crashes on MySQL due to not dropping the PonyRider.pony foreign key
        constraint before modifying the column.
        """
        app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
        project_state = self.apply_operations(app_label, ProjectState(), operations=[
            migrations.CreateModel('Rider', fields=[
                ('id', models.CharField(primary_key=True, max_length=100)),
            ]),
            migrations.CreateModel('Pony', fields=[
                ('id', models.CharField(primary_key=True, max_length=100)),
                ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
            ]),
            migrations.CreateModel('PonyRider', fields=[
                ('id', models.AutoField(primary_key=True)),
                ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
            ]),
        ])
        project_state = self.apply_operations(app_label, project_state, operations=[
            migrations.RenameField('Rider', 'id', 'id2'),
            migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
        ], atomic=connection.features.supports_atomic_references_rename) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:26,代碼來源:test_operations.py

示例4: reduce_rename_field_self

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def reduce_rename_field_self(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.new_name_lower == other.old_name_lower):
            return [
                migrations.RenameField(
                    operation.model_name,
                    operation.old_name,
                    other.new_name,
                ),
            ]

    # THROUGH CHECKS 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:14,代碼來源:optimizer.py

示例5: test_create_model_rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_create_model_rename_field(self):
        """
        RenameField should optimize into CreateModel.
        """
        managers = [('objects', EmptyManager())]
        self.assertOptimizesTo(
            [
                migrations.CreateModel(
                    name="Foo",
                    fields=[("name", models.CharField(max_length=255))],
                    options={'verbose_name': 'Foo'},
                    bases=(UnicodeModel,),
                    managers=managers,
                ),
                migrations.RenameField("Foo", "name", "title"),
            ],
            [
                migrations.CreateModel(
                    name="Foo",
                    fields=[
                        ("title", models.CharField(max_length=255)),
                    ],
                    options={'verbose_name': 'Foo'},
                    bases=(UnicodeModel,),
                    managers=managers,
                ),
            ],
        ) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:30,代碼來源:test_optimizer.py

示例6: test_add_field_rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_add_field_rename_field(self):
        """
        RenameField should optimize into AddField
        """
        self.assertOptimizesTo(
            [
                migrations.AddField("Foo", "name", models.CharField(max_length=255)),
                migrations.RenameField("Foo", "name", "title"),
            ],
            [
                migrations.AddField("Foo", "title", models.CharField(max_length=255)),
            ],
        ) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:15,代碼來源:test_optimizer.py

示例7: test_rename_m2m_model_after_rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_rename_m2m_model_after_rename_field(self):
        """RenameModel renames a many-to-many column after a RenameField."""
        app_label = 'test_rename_multiple'
        project_state = self.apply_operations(app_label, ProjectState(), operations=[
            migrations.CreateModel('Pony', fields=[
                ('id', models.AutoField(primary_key=True)),
                ('name', models.CharField(max_length=20)),
            ]),
            migrations.CreateModel('Rider', fields=[
                ('id', models.AutoField(primary_key=True)),
                ('pony', models.ForeignKey('test_rename_multiple.Pony', models.CASCADE)),
            ]),
            migrations.CreateModel('PonyRider', fields=[
                ('id', models.AutoField(primary_key=True)),
                ('riders', models.ManyToManyField('Rider')),
            ]),
            migrations.RenameField(model_name='pony', old_name='name', new_name='fancy_name'),
            migrations.RenameModel(old_name='Rider', new_name='Jockey'),
        ], atomic=connection.features.supports_atomic_references_rename)
        Pony = project_state.apps.get_model(app_label, 'Pony')
        Jockey = project_state.apps.get_model(app_label, 'Jockey')
        PonyRider = project_state.apps.get_model(app_label, 'PonyRider')
        # No "no such column" error means the column was renamed correctly.
        pony = Pony.objects.create(fancy_name='a good name')
        jockey = Jockey.objects.create(pony=pony)
        ponyrider = PonyRider.objects.create()
        ponyrider.riders.add(jockey) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:29,代碼來源:test_operations.py

示例8: test_rename_missing_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_rename_missing_field(self):
        state = ProjectState()
        state.add_model(ModelState('app', 'model', []))
        with self.assertRaisesMessage(FieldDoesNotExist, "app.model has no field named 'field'"):
            migrations.RenameField('model', 'field', 'new_field').state_forwards('app', state) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:7,代碼來源:test_operations.py

示例9: test_rename_referenced_field_state_forward

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_rename_referenced_field_state_forward(self):
        state = ProjectState()
        state.add_model(ModelState('app', 'Model', [
            ('id', models.AutoField(primary_key=True)),
            ('field', models.IntegerField(unique=True)),
        ]))
        state.add_model(ModelState('app', 'OtherModel', [
            ('id', models.AutoField(primary_key=True)),
            ('fk', models.ForeignKey('Model', models.CASCADE, to_field='field')),
            ('fo', models.ForeignObject('Model', models.CASCADE, from_fields=('fk',), to_fields=('field',))),
        ]))
        operation = migrations.RenameField('Model', 'field', 'renamed')
        new_state = state.clone()
        operation.state_forwards('app', new_state)
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ['self'])
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
        self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('fk',))
        self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',))
        operation = migrations.RenameField('OtherModel', 'fk', 'renamed_fk')
        new_state = state.clone()
        operation.state_forwards('app', new_state)
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ('self',))
        self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
        self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('renamed_fk',))
        self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',)) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:29,代碼來源:test_operations.py

示例10: rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def rename_field(field, filters: List[str]):
    """Renames a field from one name to the other.

    Arguments:
        field:
            Field to be renamed.

        filters:
            List of strings to filter
            SQL statements on.
    """

    model = define_fake_model({"title": field})
    state = migrations.state.ProjectState.from_apps(apps)

    apply_migration(
        [
            migrations.CreateModel(
                model.__name__, fields=[("title", field.clone())]
            )
        ],
        state,
    )

    with filtered_schema_editor(*filters) as calls:
        apply_migration(
            [migrations.RenameField(model.__name__, "title", "newtitle")], state
        )

    yield calls 
開發者ID:SectorLabs,項目名稱:django-postgres-extra,代碼行數:32,代碼來源:migrations.py

示例11: test_rename_field

# 需要導入模塊: from django.db import migrations [as 別名]
# 或者: from django.db.migrations import RenameField [as 別名]
def test_rename_field(self):
        """
        Tests the RenameField operation.
        """
        project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
        # Test the state alteration
        operation = migrations.RenameField("Pony", "pink", "blue")
        self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
        new_state = project_state.clone()
        operation.state_forwards("test_rnfl", new_state)
        self.assertIn("blue", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
        self.assertNotIn("pink", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
        # Make sure the unique_together has the renamed column too
        self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
        self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
        # Make sure the index_together has the renamed column too
        self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['index_together'][0])
        self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['index_together'][0])
        # Test the database alteration
        self.assertColumnExists("test_rnfl_pony", "pink")
        self.assertColumnNotExists("test_rnfl_pony", "blue")
        with connection.schema_editor() as editor:
            operation.database_forwards("test_rnfl", editor, project_state, new_state)
        self.assertColumnExists("test_rnfl_pony", "blue")
        self.assertColumnNotExists("test_rnfl_pony", "pink")
        # Ensure the unique constraint has been ported over
        with connection.cursor() as cursor:
            cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
            with self.assertRaises(IntegrityError):
                with atomic():
                    cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
            cursor.execute("DELETE FROM test_rnfl_pony")
        # Ensure the index constraint has been ported over
        self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
        # And test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards("test_rnfl", editor, new_state, project_state)
        self.assertColumnExists("test_rnfl_pony", "pink")
        self.assertColumnNotExists("test_rnfl_pony", "blue")
        # Ensure the index constraint has been reset
        self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
        # And deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], "RenameField")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"}) 
開發者ID:denisenkom,項目名稱:django-sqlserver,代碼行數:48,代碼來源:test_operations.py


注:本文中的django.db.migrations.RenameField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。