本文整理匯總了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()
示例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()
示例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()
示例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))
示例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]
示例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]