title: Dynamic views in Django ORM - best approach?
body: <p><strong>Some background</strong></p>
  
  <p>I am considering rebuilding an existing Laravel website with Django. It's a website that allows sharing benchmark data from drone/UAV propulsion components. Some benchmarks are done while testing multiple motors and propellers at the same time, which means a battery would be under the load of multiple motors, but it also means the airflow from one propeller has an impact on the data captured on the other propeller. This means the data is physically coupled. <a href="https://database.rcbenchmark.com/tests/3nq/coax-xoar-ta130-47x10-and-ta110-40x10" rel="nofollow noreferrer">Here is an example</a>. Right now I am trying to structure this project to allow upcoming features, and to see if Django ORM is a good fit.</p>
  
  <p><strong>Simplified Django models</strong></p>
  
  <pre><code>class Benchmark(models.Model):
      title = models.CharField()
      private = models.BooleanField(default=False)
      hide_torque = models.BooleanField(default=False)
  
  
  class AbstractComponent(models.Model):
      brand = models.CharField()
      name = models.CharField()
  
      class Meta:
          abstract = True
  
  
  class Motor(AbstractComponent):
      shaft_diameter_mm = models.FloatField()
  
  
  class Propeller(AbstractComponent):
      diameter_in = models.FloatField()
  
  
  class Battery(AbstractComponent):
      capacity_kwh = models.FloatField()
  
  
  class Powertrain(models.Model):
      benchmark = models.ForeignKey(Benchmark, on_delete=models.CASCADE, related_name='powertrains')
      motor = models.ForeignKey(Motor, on_delete=models.CASCADE)
      propeller = models.ForeignKey(Propeller, on_delete=models.CASCADE, blank=True, null=True)
      battery = models.ForeignKey(Battery, on_delete=models.CASCADE, blank=True, null=True)
  
  
  class DerivedDataManager(models.Manager):
      def get_queryset(self):
          return super().get_queryset()\
              .annotate(electrical_power_w=F('voltage_v') * F('current_a'))\
              .annotate(mechanical_power_w=F('torque_nm') * F('rotation_speed_rad_per_s'))\
              .annotate(motor_efficiency=F('mechanical_power_w') / F('electrical_power_w'))
  
  
  class DataSample(models.Model):
      powertrain = models.ForeignKey(Powertrain, on_delete=models.CASCADE, related_name='data')
      time_s = models.FloatField()
      voltage_v = models.FloatField(blank=True, null=True)
      current_a = models.FloatField(blank=True, null=True)
      rotation_speed_rad_per_s = models.FloatField(blank=True, null=True)
      torque_nm = models.FloatField(blank=True, null=True)
      thrust_n = models.FloatField(blank=True, null=True)
  
      objects = models.Manager()
      derived = DerivedDataManager()
  
      class Meta:
          constraints = [
              models.UniqueConstraint(fields=['powertrain', 'time_s'], name='unique temporal sample')
          ]
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>I was able to add "derived" measurements, like electrical_power_w to each row of the data, but I have no clue on how can I add derived measurements that combines the data of multiple drive trains within the same benchmark: </p>
  
  <p>Assuming 3 powertrains, each with their own voltage and current data, how can I do:</p>
  
  <p>Total_power = powertrain1.power + powertrain2.power + powertrain3.power</p>
  
  <p>for each individual timestamp (time_s)? A total power is only meaningul if the Sum is made on simultaneously taken samples.</p>
  
  <p><strong>Goal</strong></p>
  
  <p>Without loading all the database data in Django, I would eventually want to get the 5 top benchmarks in terms of maximum total power, taking into account some business logic:</p>
  
  <ul>
  <li>benchmarks marked as private are automatically excluded (until auth comes in)</li>
  <li>benchmarks that opt to hide the torque automatically make the torque data, along as all the derived mechanical power and motor efficiency values go to None.</li>
  </ul>
  
  <p>I would like to recreate <a href="https://database.rcbenchmark.com/tests" rel="nofollow noreferrer">this table</a>, but with extra columns appended, like 'maximum thrust', etc... This table is paginated from within the database itself.</p>