當前位置: 首頁>>代碼示例>>Python>>正文


Python query.Query類代碼示例

本文整理匯總了Python中django.db.models.sql.query.Query的典型用法代碼示例。如果您正苦於以下問題:Python Query類的具體用法?Python Query怎麽用?Python Query使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Query類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

 def __init__(self, model, query=None, using=None):
     # the model needs to be defined so that we can construct our custom
     # query
     if query is None:
         query = Query(model)
         query.add_q(models.Q(effective_to__isnull=True))
     return super(ActiveQuerySet, self).__init__(model, query, using)
開發者ID:parksandwildlife,項目名稱:pbs,代碼行數:7,代碼來源:managers.py

示例2: _get_field

def _get_field(model, name):
    # Create a fake query object so we can easily work out what field
    # type we are dealing with
    qs = Query(model)
    parts = name.split(LOOKUP_SEP)

    # The following is borrowed from the innards of Query.add_filter - it strips out __gt, __exact et al.
    num_parts = len(parts)
    if num_parts > 1 and parts[-1] in QUERY_TERMS:
        # Traverse the lookup query to distinguish related fields from
        # lookup types.
        for counter, field_name in enumerate(parts, 1):
            try:
                lookup_field = model._meta.get_field(field_name)
            except FieldDoesNotExist:
                # Not a field. Bail out.
                parts.pop()
                break
            # Unless we're at the end of the list of lookups, let's attempt
            # to continue traversing relations.
            if counter < num_parts:
                try:
                    model = lookup_field.rel.to
                except AttributeError:
                    # Not a related field. Bail out.
                    parts.pop()
                    break

    return qs.names_to_path(parts, qs.get_meta(), True, fail_on_missing=False)[1]
開發者ID:django4life,項目名稱:django-money,代碼行數:29,代碼來源:managers.py

示例3: test_simple_query

 def test_simple_query(self):
     query = Query(Author)
     where = query.build_where(Q(num__gt=2))
     lookup = where.children[0]
     self.assertIsInstance(lookup, GreaterThan)
     self.assertEqual(lookup.rhs, 2)
     self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:7,代碼來源:test_query.py

示例4: _get_condition_sql

 def _get_condition_sql(self, model, schema_editor):
     if self.condition is None:
         return None
     query = Query(model=model)
     where = query.build_where(self.condition)
     compiler = query.get_compiler(connection=schema_editor.connection)
     sql, params = where.as_sql(compiler, schema_editor.connection)
     return sql % tuple(schema_editor.quote_value(p) for p in params)
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:8,代碼來源:constraints.py

示例5: constraint_sql

 def constraint_sql(self, model, schema_editor):
     query = Query(model)
     where = query.build_where(self.check)
     connection = schema_editor.connection
     compiler = connection.ops.compiler('SQLCompiler')(query, connection, 'default')
     sql, params = where.as_sql(compiler, connection)
     params = tuple(schema_editor.quote_value(p) for p in params)
     return schema_editor.sql_check_constraint % {'check': sql % params}
開發者ID:RaphaelKimmig,項目名稱:django,代碼行數:8,代碼來源:constraints.py

示例6: test_multiple_fields

 def test_multiple_fields(self):
     query = Query(Item)
     where = query.build_where(Q(modified__gt=F('created')))
     lookup = where.children[0]
     self.assertIsInstance(lookup, GreaterThan)
     self.assertIsInstance(lookup.rhs, SimpleCol)
     self.assertIsInstance(lookup.lhs, SimpleCol)
     self.assertEqual(lookup.rhs.target, Item._meta.get_field('created'))
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:9,代碼來源:test_query.py

示例7: test_transform

 def test_transform(self):
     query = Query(Author)
     with register_lookup(CharField, Lower):
         where = query.build_where(~Q(name__lower='foo'))
     lookup = where.children[0]
     self.assertIsInstance(lookup, Exact)
     self.assertIsInstance(lookup.lhs, Lower)
     self.assertIsInstance(lookup.lhs.lhs, SimpleCol)
     self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field('name'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:9,代碼來源:test_query.py

示例8: test_negated_nullable

 def test_negated_nullable(self):
     query = Query(Item)
     where = query.build_where(~Q(modified__lt=datetime(2017, 1, 1)))
     self.assertTrue(where.negated)
     lookup = where.children[0]
     self.assertIsInstance(lookup, LessThan)
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
     lookup = where.children[1]
     self.assertIsInstance(lookup, IsNull)
     self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:10,代碼來源:test_query.py

示例9: test_foreign_key_exclusive

 def test_foreign_key_exclusive(self):
     query = Query(ObjectC)
     where = query.build_where(Q(objecta=None) | Q(objectb=None))
     a_isnull = where.children[0]
     self.assertIsInstance(a_isnull, RelatedIsNull)
     self.assertIsInstance(a_isnull.lhs, SimpleCol)
     self.assertEqual(a_isnull.lhs.target, ObjectC._meta.get_field('objecta'))
     b_isnull = where.children[1]
     self.assertIsInstance(b_isnull, RelatedIsNull)
     self.assertIsInstance(b_isnull.lhs, SimpleCol)
     self.assertEqual(b_isnull.lhs.target, ObjectC._meta.get_field('objectb'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:11,代碼來源:test_query.py

示例10: test_simplecol_query

    def test_simplecol_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2, name__isnull=False) | Q(num__lt=F('id')))

        name_isnull_lookup, num_gt_lookup = where.children[0].children
        self.assertIsInstance(num_gt_lookup, GreaterThan)
        self.assertIsInstance(num_gt_lookup.lhs, SimpleCol)
        self.assertIsInstance(name_isnull_lookup, IsNull)
        self.assertIsInstance(name_isnull_lookup.lhs, SimpleCol)

        num_lt_lookup = where.children[1]
        self.assertIsInstance(num_lt_lookup, LessThan)
        self.assertIsInstance(num_lt_lookup.rhs, SimpleCol)
        self.assertIsInstance(num_lt_lookup.lhs, SimpleCol)
開發者ID:MattBlack85,項目名稱:django,代碼行數:14,代碼來源:test_query.py

示例11: test_complex_query

    def test_complex_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2) | Q(num__lt=0))
        self.assertEqual(where.connector, OR)

        lookup = where.children[0]
        self.assertIsInstance(lookup, GreaterThan)
        self.assertEqual(lookup.rhs, 2)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))

        lookup = where.children[1]
        self.assertIsInstance(lookup, LessThan)
        self.assertEqual(lookup.rhs, 0)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:14,代碼來源:test_query.py

示例12: get_related_decrement_value

    def get_related_decrement_value(self, using):
        qn = self.get_quote_name(using)

        related_query = Query(self.manager.related.model)
        related_query.add_extra(None, None,
            ["%s = %s.%s" % (qn(self.model._meta.pk.get_attname_column()[1]), 'OLD', qn(self.manager.related.field.m2m_column_name()))],
            None, None, None)
        related_query.add_fields([self.fieldname])
        related_query.clear_ordering(force_empty=True)
        related_query.default_cols = False
        related_filter_where, related_where_params = related_query.get_compiler(using=using).as_sql()
        return "%s - (%s)" % (qn(self.fieldname), related_filter_where)
開發者ID:Kronuz,項目名稱:django-denorm,代碼行數:12,代碼來源:denorms.py

示例13: _get_field

def _get_field(model, name):
    if django.VERSION[0] >= 1 and django.VERSION[1] >= 8:
        # Django 1.8+ - can use something like 
        # expression.output_field.get_internal_field() == 'Money..'
        raise NotImplementedError("Django 1.8+ support is not implemented.")

    from django.db.models.fields import FieldDoesNotExist

    # Create a fake query object so we can easily work out what field
    # type we are dealing with
    qs = Query(model)
    opts = qs.get_meta()
    alias = qs.get_initial_alias()

    parts = name.split(LOOKUP_SEP)

    # The following is borrowed from the innards of Query.add_filter - it strips out __gt, __exact et al.
    num_parts = len(parts)
    if num_parts > 1 and parts[-1] in qs.query_terms:
        # Traverse the lookup query to distinguish related fields from
        # lookup types.
        lookup_model = model
        for counter, field_name in enumerate(parts):
            try:
                lookup_field = lookup_model._meta.get_field(field_name)
            except FieldDoesNotExist:
                # Not a field. Bail out.
                parts.pop()
                break
            # Unless we're at the end of the list of lookups, let's attempt
            # to continue traversing relations.
            if (counter + 1) < num_parts:
                try:
                    lookup_model = lookup_field.rel.to
                except AttributeError:
                    # Not a related field. Bail out.
                    parts.pop()
                    break

    if django.VERSION[0] >= 1 and django.VERSION[1] in (6, 7):
        # Django 1.6-1.7
        field = qs.setup_joins(parts, opts, alias)[0]
    else:
        # Django 1.4-1.5
        field = qs.setup_joins(parts, opts, alias, False)[0]

    return field
開發者ID:CloudRunnerInc,項目名稱:django-money,代碼行數:47,代碼來源:managers.py

示例14: test_foreign_key_f

 def test_foreign_key_f(self):
     query = Query(Ranking)
     with self.assertRaises(FieldError):
         query.build_where(Q(rank__gt=F('author__num')))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:4,代碼來源:test_query.py

示例15: test_foreign_key

 def test_foreign_key(self):
     query = Query(Item)
     msg = 'Joined field references are not permitted in this query'
     with self.assertRaisesMessage(FieldError, msg):
         query.build_where(Q(creator__num__gt=2))
開發者ID:EmadMokhtar,項目名稱:django,代碼行數:5,代碼來源:test_query.py


注:本文中的django.db.models.sql.query.Query類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。