from customers.models import *
from django.db.models.sql.datastructures import Date
from django.db.models import *
from django.db.models.fields import DateField
class AggregateDate(object):
def __init__(self, lookup, kind, **extra):
self.lookup = lookup
self.kind = kind
self.extra = extra
def _default_alias(self):
return '%s__%s' % (self.lookup, self.kind)
default_alias = property(_default_alias)
def add_to_query(self, query, alias, col, source, is_summary):
klass = Date
aggregate = klass(col, self.kind, source=source, is_summary=is_summary, **self.extra)
query.add_filter(("%s__isnull" % self.lookup, False))
query.aggregates[alias] = aggregate
class Date(object):
is_ordinal = False
is_computed = False
def __init__(self, col, lookup_type, source=None, is_summary=None, **extra):
self.col = col
self.lookup_type = lookup_type
self.source = source
self.is_summary = is_summary
self.extra = extra
self.field = DateField() # May need to change
def relabeled_clone(self, change_map):
return self.__class__((change_map.get(self.col[0], self.col[0]), self.col[1]))
def as_sql(self, qn, connection):
if isinstance(self.col, (list, tuple)):
col = '%s.%s' % tuple([qn(c) for c in self.col])
else:
col = self.col
return connection.ops.date_trunc_sql(self.lookup_type, col), []
PersonCustomer.objects.all().annotate(day=AggregateDate("edited", "year")).values("day")
-
Bitte melde Dich an, um einen Kommentar zu verfassen!