本文整理汇总了Python中django.db.models.query.RawQuerySet方法的典型用法代码示例。如果您正苦于以下问题:Python query.RawQuerySet方法的具体用法?Python query.RawQuerySet怎么用?Python query.RawQuerySet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.query
的用法示例。
在下文中一共展示了query.RawQuerySet方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_qs
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def prepare_qs(self):
self.wrapped_serializer = self.__class__.object_serializer_class(self.request)
self.wrapped_serializer.set_qs(self.queryset_copy())
prepared_qs = self.wrapped_serializer.prepare_qs()
if isinstance(prepared_qs, RawQuerySet):
prepared_qs.query.sql += " OFFSET %d LIMIT %d" % (self.offset, self.limit + 1)
paginated_results_plus_one_object = list(prepared_qs)
elif isinstance(prepared_qs, list):
paginated_results_plus_one_object = prepared_qs[self.offset:self.offset + self.limit + 1]
else:
paginated_results_plus_one_object = list(prepared_qs[self.offset:self.offset + self.limit + 1])
if len(paginated_results_plus_one_object) > self.limit:
there_is_more = True
paginated_results = paginated_results_plus_one_object[:-1]
else:
there_is_more = False
paginated_results = paginated_results_plus_one_object
return [{
'there_is_more': there_is_more,
'objects': self.wrapped_serializer.serialize_qs(paginated_results)
}]
示例2: raw
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def raw(self, raw_query, params=None, *args, **kwargs):
return RawQuerySet(raw_query=raw_query, model=self.model, params=params, using=self._db, *args, **kwargs)
示例3: test_rawqueryset_repr
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def test_rawqueryset_repr(self):
queryset = RawQuerySet(raw_query='SELECT * FROM raw_query_author')
self.assertEqual(repr(queryset), '<RawQuerySet: SELECT * FROM raw_query_author>')
self.assertEqual(repr(queryset.query), '<RawQuery: SELECT * FROM raw_query_author>')
示例4: test_query_representation
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def test_query_representation(self):
"""
Test representation of raw query with parameters
"""
query = "SELECT * FROM raw_query_author WHERE last_name = %(last)s"
qset = Author.objects.raw(query, {'last': 'foo'})
self.assertEqual(repr(qset), "<RawQuerySet: SELECT * FROM raw_query_author WHERE last_name = foo>")
self.assertEqual(repr(qset.query), "<RawQuery: SELECT * FROM raw_query_author WHERE last_name = foo>")
query = "SELECT * FROM raw_query_author WHERE last_name = %s"
qset = Author.objects.raw(query, {'foo'})
self.assertEqual(repr(qset), "<RawQuerySet: SELECT * FROM raw_query_author WHERE last_name = foo>")
self.assertEqual(repr(qset.query), "<RawQuery: SELECT * FROM raw_query_author WHERE last_name = foo>")
示例5: populate_many_relation
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def populate_many_relation(feature_class, related_field_name, related_field_configuration,
query=None, filter_dict=None, extra=None, custom_join=None,
join_on_base=False, join_related_on_base=False):
"""
Populate an association table that is associated with the given feature_class.
You must specify source_field_name and field_name_on_related_class to join on those two values, or specify a query to skip the join process
:param feature_class:
:param related_field_name: The field name of the feature_class that leads to the associated class.
This doesn't have to be a real column on the table, but it must be modeled as a ForeignKey, ManyToMany, etc
:param related_field_configuration. Configuration dict containing:
source_class_join_field_name: Optional The name of the feature_class field whose value will be used to join with the associated class table
related_class_join_field_name: Optional The field name of the feature_class that points to the association
:param query: Optional query that contains the data fro bulk insert
:param filter_dict: Optional filter for the query
:param extra: For where clauses on the join table
:param custom_join: regex string to replace the normal table1.field1 = table2.field2 join clause. The two table.field segments are captured by a regex and put in the custom_join
regex. So the custom_join must be a regex and include a \1 and \2.
:param join_on_base: Join the feature_class using its base class (not its rel class)
:param join_related_on_base: Join the related_class using its base class (not its rel class)
:return:
"""
relationship = ManyJoinRelationship(feature_class, related_field_name, related_field_configuration,
query=query, filter_dict=filter_dict, extra=extra, custom_join=custom_join,
join_on_base=join_on_base, join_related_on_base=join_related_on_base)
# Create the through table if doesn't yet exist. This would only happen for an explicit through class
create_tables_for_dynamic_classes(relationship.through_class)
# Quit if the table is already populated
if relationship.through_class.objects.count() > 0:
return
queryset = get_related_results(
feature_class,
relationship,
return_queryset=True,
field_names=['related_pk', 'pk'])
through_table_columns = [
# TODO this order corresponds with the order of the queryset selection. It shouldn't be so arbitrary
relationship.through_class_related_column_name,
relationship.through_class_self_column_name]
insert_sql = 'insert into {through_table} ({through_table_columns}) {select_query}'.format(
through_table=relationship.through_class._meta.db_table,
through_table_columns=','.join(through_table_columns),
select_query=queryset.query.sql if isinstance(queryset, RawQuerySet) else queryset.query
)
logger.info("Populating through table {through_table} between {feature_class_table} and {related_class_table}".format(
through_table=relationship.through_class._meta.db_table,
feature_class_table=feature_class._meta.db_table,
related_class_table=relationship.related_class._meta.db_table
))
logger.info("Using insert sql: {insert_sql}".format(insert_sql=insert_sql))
conn = psycopg2.connect(**pg_connection_parameters(settings.DATABASES['default']))
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
result = cursor.execute(insert_sql)
示例6: fill_geography_association_from_source
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import RawQuerySet [as 别名]
def fill_geography_association_from_source(source_feature_class, destination_feature_class, related_field_configuration,
related_field_name, no_populate):
# Construct a class instance that models the ManyToMany relationship through the related field for the source
# class
source_relationship = ManyJoinRelationship(
related_field_configuration['source']['source_class'],
related_field_name,
related_field_configuration['source']
)
# Construct a class instance that models the ManyToMany relationship through the related field for the destination class
relationship = ManyJoinRelationship(
related_field_configuration['destination']['source_class'],
related_field_name,
related_field_configuration['destination']
)
create_tables_for_dynamic_classes(relationship.through_class)
if relationship.through_class.objects.count() > 0 or no_populate:
# Already populated this through class table
pass
# Fetch the source through instance queryset limited to the features in the destination
queryset = source_relationship.through_class.objects
if destination_feature_class.objects.count() != source_feature_class.objects.count():
queryset = queryset.filter(
**{'{0}__pk__in'.format(
source_relationship.through_class_self_field.name): destination_feature_class.objects.values_list('id',
flat=True)}
)
queryset = queryset.values(
source_relationship.through_class_related_field.name,
source_relationship.through_class_self_field.name
)
through_table_columns = [
# TODO this order corresponds with the order of the queryset selection. It shouldn't be so arbitrary
relationship.through_class_related_column_name,
relationship.through_class_self_column_name]
insert_sql = 'insert into {through_table} ({through_table_columns}) {select_query}'.format(
through_table=relationship.through_class._meta.db_table,
through_table_columns=','.join(through_table_columns),
select_query=queryset.query.sql if isinstance(queryset, RawQuerySet) else queryset.query
)
logger.info(
"Populating through table {through_table} between {feature_class_table} and {related_class_table}".format(
through_table=relationship.through_class._meta.db_table,
feature_class_table=destination_feature_class._meta.db_table,
related_class_table=relationship.related_class._meta.db_table
))
logger.info("Using insert sql: {insert_sql}".format(insert_sql=insert_sql))
conn = psycopg2.connect(**pg_connection_parameters(settings.DATABASES['default']))
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
result = cursor.execute(insert_sql)