def test(self): assert len(Dummy.objects.all()) == 3 assert len(Dummy.objects.annotate(temp=SF("temp"))) == 3 assert len( Dummy.objects.annotate(temp=SF("temp")).order_by("temp")) == 3 assert Dummy.objects.annotate( temp=SF("temp")).order_by("temp").first().temp == 1 assert Dummy.objects.annotate( temp=SF("temp")).order_by("-temp").first().temp == 3
def test(self): Dummy.objects.create(temp=1, check_test=10) Dummy.objects.create(temp=2, check_test=10) Dummy.objects.create(temp=1, check_test=20) counts = list( Dummy.objects.values("temp", "check_test").annotate( count=Count("*")).order_by("temp", "check_test")) assert len(counts) == 3 assert counts == [ { "temp": 1, "check_test": 10, "count": 1 }, { "temp": 1, "check_test": 20, "count": 1 }, { "temp": 2, "check_test": 10, "count": 1 }, ] counts = list( Dummy.objects.values("temp").annotate( sum=Sum(SF("check_test"))).order_by("temp")) assert len(counts) == 2 assert counts == [{"temp": 1, "sum": 30}, {"temp": 2, "sum": 10}]
def values(self, *fields, **expressions): qs = self for f in fields: if f in self.model.subfield_names: subfield_name, _ = self.model.resolve_subfield_filter_value(f) if subfield_name not in self.query.annotations: qs = qs.annotate(**{subfield_name: SF(subfield_name)}) return qs._super_values(*fields, **expressions)
def test(self): Dummy.objects.create(temp=1) Dummy.objects.create(temp=2) Dummy.objects.create(temp=1) qs = Dummy.objects.annotate(temp=SF("temp")) assert str(qs.query).find("GROUP BY") == -1 assert qs._result_cache is None assert len(qs) == 3 assert qs._result_cache qs = qs.values("temp") assert qs._result_cache is None assert str(qs.query).find("GROUP BY") == -1 assert len(qs) == 3 assert qs._result_cache qs = qs.annotate(count=Count("*")) assert qs._result_cache is None assert str(qs.query).find("GROUP BY") != -1 assert len(qs) == 2 assert qs._result_cache
def test(self): instance = Dummy.objects.create(temp=1) assert str(Dummy.objects.annotate(temp=SF("temp"))) assert instance.temp == 1 assert instance.data["temp"] == 1
def test(self): Dummy.objects.create() with self.assertRaises(AssertionError): str(Dummy.objects.annotate(uname=SF("temp"))) with self.assertRaises(ValueError): str(Dummy.objects.annotate(version=SF("temp")))
def test(self): Dummy.objects.create() assert str(Dummy.objects.annotate(uname=SF("uname")))
def sum(self, subfield_name): subfield_name, _ = self.model.resolve_subfield_filter_value(subfield_name) annotated = self.annotate(**{subfield_name: SF(subfield_name)}) result = annotated.aggregate(Sum(subfield_name))["{}__sum".format(subfield_name)] or 0 return result