当前位置: 首页>>代码示例>>Python>>正文


Python ForeignKey.set_attributes_from_name方法代码示例

本文整理汇总了Python中django.db.models.fields.related.ForeignKey.set_attributes_from_name方法的典型用法代码示例。如果您正苦于以下问题:Python ForeignKey.set_attributes_from_name方法的具体用法?Python ForeignKey.set_attributes_from_name怎么用?Python ForeignKey.set_attributes_from_name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.db.models.fields.related.ForeignKey的用法示例。


在下文中一共展示了ForeignKey.set_attributes_from_name方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_fk

# 需要导入模块: from django.db.models.fields.related import ForeignKey [as 别名]
# 或者: from django.db.models.fields.related.ForeignKey import set_attributes_from_name [as 别名]
 def test_fk(self):
     "Tests that creating tables out of FK order, then repointing, works"
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Book)
         editor.create_model(Author)
         editor.create_model(Tag)
     # Check that initial tables are there
     list(Author.objects.all())
     list(Book.objects.all())
     # Make sure the FK constraint is present
     with self.assertRaises(IntegrityError):
         Book.objects.create(
             author_id=1,
             title="Much Ado About Foreign Keys",
             pub_date=datetime.datetime.now(),
         )
     # Repoint the FK constraint
     new_field = ForeignKey(Tag)
     new_field.set_attributes_from_name("author")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Book,
             Book._meta.get_field_by_name("author")[0],
             new_field,
             strict=True,
         )
     # Make sure the new FK constraint is present
     constraints = self.get_constraints(Book._meta.db_table)
     for name, details in constraints.items():
         if details['columns'] == ["author_id"] and details['foreign_key']:
             self.assertEqual(details['foreign_key'], ('schema_tag', 'id'))
             break
     else:
         self.fail("No FK constraint for author_id found")
开发者ID:DasAllFolks,项目名称:django,代码行数:37,代码来源:tests.py

示例2: test_no_index_for_foreignkey

# 需要导入模块: from django.db.models.fields.related import ForeignKey [as 别名]
# 或者: from django.db.models.fields.related.ForeignKey import set_attributes_from_name [as 别名]
    def test_no_index_for_foreignkey(self):
        """
        MySQL on InnoDB already creates indexes automatically for foreign keys.
        (#14180). An index should be created if db_constraint=False (#26171).
        """
        storage = connection.introspection.get_storage_engine(
            connection.cursor(), ArticleTranslation._meta.db_table
        )
        if storage != "InnoDB":
            self.skip("This test only applies to the InnoDB storage engine")
        index_sql = connection.schema_editor()._model_indexes_sql(ArticleTranslation)
        self.assertEqual(index_sql, [
            'CREATE INDEX `indexes_articletranslation_article_no_constraint_id_d6c0806b` '
            'ON `indexes_articletranslation` (`article_no_constraint_id`)'
        ])

        # The index also shouldn't be created if the ForeignKey is added after
        # the model was created.
        with connection.schema_editor() as editor:
            new_field = ForeignKey(Article, CASCADE)
            new_field.set_attributes_from_name('new_foreign_key')
            editor.add_field(ArticleTranslation, new_field)
            self.assertEqual(editor.deferred_sql, [
                'ALTER TABLE `indexes_articletranslation` '
                'ADD CONSTRAINT `indexes_articletrans_new_foreign_key_id_d27a9146_fk_indexes_a` '
                'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)'
            ])
开发者ID:japrogramer,项目名称:django,代码行数:29,代码来源:tests.py

示例3: test_no_index_for_foreignkey

# 需要导入模块: from django.db.models.fields.related import ForeignKey [as 别名]
# 或者: from django.db.models.fields.related.ForeignKey import set_attributes_from_name [as 别名]
    def test_no_index_for_foreignkey(self):
        """
        MySQL on InnoDB already creates indexes automatically for foreign keys.
        (#14180). An index should be created if db_constraint=False (#26171).
        """
        storage = connection.introspection.get_storage_engine(
            connection.cursor(), ArticleTranslation._meta.db_table
        )
        if storage != "InnoDB":
            self.skip("This test only applies to the InnoDB storage engine")
        index_sql = [str(statement) for statement in connection.schema_editor()._model_indexes_sql(ArticleTranslation)]
        self.assertEqual(index_sql, [
            'CREATE INDEX `indexes_articletranslation_article_no_constraint_id_d6c0806b` '
            'ON `indexes_articletranslation` (`article_no_constraint_id`)'
        ])

        # The index also shouldn't be created if the ForeignKey is added after
        # the model was created.
        field_created = False
        try:
            with connection.schema_editor() as editor:
                new_field = ForeignKey(Article, CASCADE)
                new_field.set_attributes_from_name('new_foreign_key')
                editor.add_field(ArticleTranslation, new_field)
                field_created = True
                # No deferred SQL. The FK constraint is included in the
                # statement to add the field.
                self.assertFalse(editor.deferred_sql)
        finally:
            if field_created:
                with connection.schema_editor() as editor:
                    editor.remove_field(ArticleTranslation, new_field)
开发者ID:EmadMokhtar,项目名称:django,代码行数:34,代码来源:tests.py

示例4: test_fk_index_creation

# 需要导入模块: from django.db.models.fields.related import ForeignKey [as 别名]
# 或者: from django.db.models.fields.related.ForeignKey import set_attributes_from_name [as 别名]
 def test_fk_index_creation(self):
     new_field = ForeignKey(Foo)
     new_field.set_attributes_from_name(None)
     with connection.schema_editor() as editor:
         editor.add_field(
             Bar,
             new_field
         )
         # Just return indexes others that not automaically created by Fk
         indexes = editor._get_field_indexes(Bar, new_field)
     self.assertEqual(indexes, [])
开发者ID:maxirobaina,项目名称:django-firebird,代码行数:13,代码来源:tests.py


注:本文中的django.db.models.fields.related.ForeignKey.set_attributes_from_name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。