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


Python query.RawQuerySet方法代码示例

本文整理汇总了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)
        }] 
开发者ID:KlubJagiellonski,项目名称:Politikon,代码行数:26,代码来源:serializers.py

示例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) 
开发者ID:blackye,项目名称:luscan-devel,代码行数:4,代码来源:manager.py

示例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>') 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:6,代码来源:tests.py

示例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>") 
开发者ID:denisenkom,项目名称:django-sqlserver,代码行数:15,代码来源:tests.py

示例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) 
开发者ID:CalthorpeAnalytics,项目名称:urbanfootprint,代码行数:62,代码来源:data_import_publishing.py

示例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) 
开发者ID:CalthorpeAnalytics,项目名称:urbanfootprint,代码行数:54,代码来源:data_import_publishing.py


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