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


Python query_utils.InvalidQuery方法代码示例

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


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

示例1: __iter__

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def __iter__(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()
            if self.model._meta.pk.attname not in model_init_names:
                raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            if converters:
                query = compiler.apply_converters(query, converters)
            for values in query:
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:34,代码来源:query.py

示例2: iterator

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def iterator(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()
            if self.model._meta.pk.attname not in model_init_names:
                raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            if converters:
                query = compiler.apply_converters(query, converters)
            for values in query:
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:34,代码来源:query.py

示例3: __iter__

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def __iter__(self):
        # Cache some things for performance reasons outside the loop.
        db = self.db
        compiler = connections[db].ops.compiler('SQLCompiler')(
            self.query, connections[db], db
        )

        query = iter(self.query)

        try:
            model_init_names, model_init_pos, annotation_fields = self.resolve_model_init_order()

            # Find out which model's fields are not present in the query.
            skip = set()
            for field in self.model._meta.fields:
                if field.attname not in model_init_names:
                    skip.add(field.attname)
            if skip:
                if self.model._meta.pk.attname in skip:
                    raise InvalidQuery('Raw query must include the primary key')
            model_cls = self.model
            fields = [self.model_fields.get(c) for c in self.columns]
            converters = compiler.get_converters([
                f.get_col(f.model._meta.db_table) if f else None for f in fields
            ])
            for values in query:
                if converters:
                    values = compiler.apply_converters(values, converters)
                # Associate fields to values
                model_init_values = [values[pos] for pos in model_init_pos]
                instance = model_cls.from_db(db, model_init_names, model_init_values)
                if annotation_fields:
                    for column, pos in annotation_fields:
                        setattr(instance, column, values[pos])
                yield instance
        finally:
            # Done iterating the Query. If it has its own cursor, close it.
            if hasattr(self.query, 'cursor') and self.query.cursor:
                self.query.cursor.close() 
开发者ID:Yeah-Kun,项目名称:python,代码行数:41,代码来源:query.py

示例4: test_missing_fields_without_PK

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def test_missing_fields_without_PK(self):
        query = "SELECT first_name, dob FROM raw_query_author"
        with self.assertRaisesMessage(InvalidQuery, 'Raw query must include the primary key'):
            list(Author.objects.raw(query)) 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:6,代码来源:tests.py

示例5: test_defer_select_related_raises_invalid_query

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def test_defer_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed '
            'using select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.defer("related").select_related("related")[0] 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:9,代码来源:tests.py

示例6: test_only_select_related_raises_invalid_query

# 需要导入模块: from django.db.models import query_utils [as 别名]
# 或者: from django.db.models.query_utils import InvalidQuery [as 别名]
def test_only_select_related_raises_invalid_query(self):
        msg = (
            'Field Primary.related cannot be both deferred and traversed using '
            'select_related at the same time.'
        )
        with self.assertRaisesMessage(InvalidQuery, msg):
            Primary.objects.only("name").select_related("related")[0] 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:9,代码来源:tests.py


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