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


Python QuerySet.order_by_raw方法代码示例

本文整理汇总了Python中eulexistdb.query.QuerySet.order_by_raw方法的典型用法代码示例。如果您正苦于以下问题:Python QuerySet.order_by_raw方法的具体用法?Python QuerySet.order_by_raw怎么用?Python QuerySet.order_by_raw使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在eulexistdb.query.QuerySet的用法示例。


在下文中一共展示了QuerySet.order_by_raw方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ExistQueryTest

# 需要导入模块: from eulexistdb.query import QuerySet [as 别名]
# 或者: from eulexistdb.query.QuerySet import order_by_raw [as 别名]

#.........这里部分代码省略.........
        self.assertEqual(NUM_FIXTURES, self.qs.count(), "main queryset remains unchanged by filter")

    def test_order_by(self):
        # element
        fqs = self.qs.order_by('name')
        self.assertEqual('four', fqs[0].name)
        self.assertEqual('one', fqs[1].name)
        self.assertEqual('three', fqs[2].name)
        self.assertEqual('two', fqs[3].name)
        self.assert_('order by ' not in self.qs.query.getQuery(), "main queryset unchanged by order_by()")
        # attribute
        fqs = self.qs.order_by('id')
        self.assertEqual('abc', fqs[0].id)
        self.assertEqual('def', fqs[1].id)
        self.assertEqual('one', fqs[2].id)
        self.assertEqual('xyz', fqs[3].id)
        # reverse sorting
        fqs = self.qs.order_by('-name')
        self.assertEqual('four', fqs[3].name)
        self.assertEqual('two', fqs[0].name)
        fqs = self.qs.order_by('-id')
        self.assertEqual('abc', fqs[3].id)
        self.assertEqual('xyz', fqs[0].id)
        # case-insensitive sorting - upper-case description should not sort first
        fqs = self.qs.order_by('~description')
        self.assert_(fqs[0].description.startswith('third'))
        self.assert_(fqs[1].description.startswith('This one contains'))
        # reverse case-insensitive sorting - flags in either order
        fqs = self.qs.order_by('~-description')
        self.assert_(fqs[3].description.startswith('third'))
        fqs = self.qs.order_by('-~description')
        self.assert_(fqs[3].description.startswith('third'))

    def test_order_by_raw(self):
        fqs = self.qs.order_by_raw('min(%(xq_var)s/year)')
        self.assert_('1990' in fqs[0].years)
        self.assert_('2001' in fqs[1].years)
        self.assert_('2010' in fqs[2].years)
        self.assertEqual([], fqs[3].years)

        fqs = self.qs.order_by_raw('min(%(xq_var)s/year)', ascending=False)
        self.assertEqual([], fqs[0].years)
        self.assert_('2010' in fqs[1].years)
        self.assert_('2001' in fqs[2].years)
        self.assert_('1990' in fqs[3].years)

    def test_only(self):
        self.qs.only('name')
        self.assert_('element name {' not in self.qs.query.getQuery(), "main queryset unchanged by only()")

        fqs = self.qs.filter(id='one').only('name', 'id', 'sub', 'or_field')
        self.assert_(isinstance(fqs[0], QueryTestModel))  # actually a Partial type derived from this
        # attributes that should be present
        self.assertNotEqual(fqs[0].id, None)
        self.assertNotEqual(fqs[0].sub, None)
        self.assertNotEqual(fqs[0].sub.subname, None)
        self.assertNotEqual(fqs[0].or_field, None)
        # attribute not returned
        self.assertEqual(fqs[0].description, None)
        self.assertEqual('one', fqs[0].id)
        self.assertEqual('one', fqs[0].name)
        self.assertEqual('la', fqs[0].sub.subname)
        self.assertEqual('one', fqs[0].or_field)    # = name (first of ORed fields present)

        fqs = self.qs.filter(id='one').only('wnn')
        self.assertTrue(hasattr(fqs[0], "wnn"))
开发者ID:mangalam-research,项目名称:eulexistdb,代码行数:70,代码来源:test_query.py


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