diff --git a/__init__.py b/__init__.py index fa83553..0e815e3 100644 --- a/__init__.py +++ b/__init__.py @@ -129,10 +129,10 @@ class Q(QMixin): if lookup in ['year', 'month', 'day' 'hour', 'minute', 'second']: if arr: - column = "DATEPART('{0}')__{1}".format(lookup, arr.pop(0)) + column = "DATEPART('{0}', {1})__{2}".format(lookup, column, arr.pop(0)) return self._process(column, value) else: - return "DATEPART('{0}')={1}".format(lookup, value) + return "DATEPART('{0}', {1})={2}".format(lookup, column, value) if lookup in self.op_map.keys(): return "{0}{1}{2}".format(column, self.op_map[lookup], self._get_value(value)) diff --git a/tests.py b/tests.py index 09d05ac..d0870e7 100644 --- a/tests.py +++ b/tests.py @@ -19,6 +19,9 @@ class TestSqlBuilder(unittest.TestCase): date = datetime.datetime(2010, 1, 15, 23, 59, 38) self.assertEqual(str(Q(fecha=date)), "(fecha='2010-01-15 23:59:38')") + self.assertEqual(str(Q(fecha__year__lte=2012)), "(DATEPART('year', fecha)<=2012)") + self.assertEqual(str(Q(fecha__year=2012)), "(DATEPART('year', fecha)=2012)") + if __name__ == '__main__': unittest.main()