Changes between Version 1 and Version 5 of Ticket #25534


Ignore:
Timestamp:
Oct 10, 2015, 3:08:05 PM (9 years ago)
Author:
Raúl Pedro Santos
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #25534 – Description

    v1 v5  
    44
    55My specific use case is this: I have a set of electricity consumption readings, each with a datetime field (and a few others). I need to sum the consumption and cost values grouped by month, day, year, week, etc. In other words, I need to be able to get the total energy consumption value and corresponding cost for each month, day, year, week, etc.
     6
     7This is my {{{ElectricityReading}}} model and its parent {{{Reading}}} model (separated because we also have consumption readings for water and gas, which also derive from {{{Reading}}}):
     8
     9{{{#!python
     10from model_utils.models import TimeStampedModel
     11# Other imports here...
     12
     13class Reading(TimeStampedModel):
     14    device = models.ForeignKey(Device)
     15    datetime = models.DateTimeField() # Terrible property name, I know :)
     16    manual = models.BooleanField(default=False)
     17    inserted_by = models.ForeignKey(User)
     18
     19    class Meta:
     20        abstract = True
     21
     22class ElectricityReading(Reading):
     23    vph1 = models.DecimalField(max_digits=18, decimal_places=3, null=True)
     24    vph2 = models.DecimalField(max_digits=18, decimal_places=3, null=True)
     25    vph3 = models.DecimalField(max_digits=18, decimal_places=3, null=True)
     26    wh_imp = models.DecimalField(max_digits=18, decimal_places=3)
     27    varh = models.DecimalField(max_digits=18, decimal_places=3, null=True)
     28    pf = models.DecimalField(max_digits=18, decimal_places=3, null=True)
     29
     30    price = models.ForeignKey(ElectricityPrice)
     31    consumption = models.DecimalField(max_digits=18, decimal_places=3,
     32                                      null=True, blank=True, default=None)
     33    cost = models.DecimalField(max_digits=18, decimal_places=3, null=True,
     34                               blank=True, default=None)
     35}}}
    636
    737I think the code I need is something along the lines of the following:
Back to Top