本文整理汇总了Python中django.db.models.sql.InsertQuery方法的典型用法代码示例。如果您正苦于以下问题:Python sql.InsertQuery方法的具体用法?Python sql.InsertQuery怎么用?Python sql.InsertQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.sql
的用法示例。
在下文中一共展示了sql.InsertQuery方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _insert
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def _insert(self, objs, fields, return_id=False, raw=False, using=None):
"""
Inserts a new record for the given model. This provides an interface to
the InsertQuery class and is how Model.save() is implemented.
"""
self._for_write = True
if using is None:
using = self.db
query = sql.InsertQuery(self.model)
query.insert_values(fields, objs, raw=raw)
return query.get_compiler(using=using).execute_sql(return_id)
示例2: _insert
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def _insert(self, objs, fields, return_id=False, raw=False, using=None):
"""
Insert a new record for the given model. This provides an interface to
the InsertQuery class and is how Model.save() is implemented.
"""
self._for_write = True
if using is None:
using = self.db
query = sql.InsertQuery(self.model)
query.insert_values(fields, objs, raw=raw)
return query.get_compiler(using=using).execute_sql(return_id)
示例3: insert_query
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def insert_query(model, objs, fields, return_id=False, raw=False, using=None):
"""
Inserts a new record for the given model. This provides an interface to
the InsertQuery class and is how Model.save() is implemented. It is not
part of the public API.
"""
query = sql.InsertQuery(model)
query.insert_values(fields, objs, raw=raw)
return query.get_compiler(using=using).execute_sql(return_id)
示例4: test_insert_uuid_field
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def test_insert_uuid_field(self):
import uuid
from django.db.models import sql
from testapp.models import TestModel
obj = TestModel(uuid=uuid.uuid4())
q = sql.InsertQuery(obj)
q.insert_values(obj._meta.local_fields, [obj])
statements = q.get_compiler('default').as_sql()
# uuid is the last field of TestModel
uuid_insert_value = statements[0][1][-1]
# the Python value for insertion must be a string whose length is 32
self.assertEqual(type(uuid_insert_value), str)
self.assertEqual(len(uuid_insert_value), 32)
示例5: test_sql_insert_compiler_return_id_attribute
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def test_sql_insert_compiler_return_id_attribute(self):
"""
Regression test for #14019: SQLInsertCompiler.as_sql() failure
"""
db = router.db_for_write(Party)
query = InsertQuery(Party)
query.insert_values([Party._meta.fields[0]], [], raw=False)
# this line will raise an AttributeError without the accompanying fix
query.get_compiler(using=db).as_sql()
示例6: chain
# 需要导入模块: from django.db.models import sql [as 别名]
# 或者: from django.db.models.sql import InsertQuery [as 别名]
def chain(self, klass=None):
"""Chains this query to another.
We override this so that we can make sure our subclassed query
classes are used.
"""
if klass == sql.UpdateQuery:
return super().chain(PostgresUpdateQuery)
if klass == sql.InsertQuery:
return super().chain(PostgresInsertQuery)
return super().chain(klass)