title: Django Create Class From Multiple Functions
body: <p>I have several functions in my utils.py file that my views use to calculate trade stats. Below I added just 3 but there are 8 and soon there will be several more. The way I currently have it set up works but it's very inefficient. You can see each function starts from the beginning and calls other functions that set similar variables. </p>
  
  <p>What is the proper way to turn this into a class? </p>
  
  <p>Is this even the right approach?</p>
  
  <hr>
  
  <p>utils.py | multiple functions</p>
  
  <pre><code>def max_amount_function (pk):
      trade = get_object_or_404(Trade, pk=pk)
      entries_buy = Entry.objects.filter(trade=trade, entry_type="entry")
      max_amount_function = entries_buy.aggregate(max_amount_function=Sum('amount'))['max_amount_function']
      if max_amount_function is None:
          return 0
      else:
          return max_amount_function
  
  def fees_function (pk):
      trade = get_object_or_404(Trade, pk=pk)
      entries = Entry.objects.filter(trade=trade)
      fees_function = entries.aggregate(fees_function=Sum('fee'))['fees_function']
      return fees_function
  
  def entry_cpu_function (pk):
      if max_amount_function(pk) &gt; 0:
          trade = get_object_or_404(Trade, pk=pk)
          entries_buy = Entry.objects.filter(trade=trade, entry_type="entry")
          entry_cpu_function = entries_buy.annotate(
              s=F('amount') * F('price')
          ).aggregate(
              entry_cpu_function=ExpressionWrapper(
                  Sum(
                      Cast('s', output_field=models.FloatField())
                  ) / Sum('amount'),
                  output_field=models.FloatField()
              )
          )['entry_cpu_function']
          return entry_cpu_function
      else:
          return 0
  </code></pre>
  
  <p>utils.py | one class</p>
  
  <pre><code>class TradeStats:
      trade = get_object_or_404(Trade)
      entries_buy = Entry.objects.filter(trade=trade, entry_type="entry")
  
      def __init__(self):
          pass
  
      def max_amount_functio(pk):
          pass
  
      def fees_function(pk):
          pass
  
      def entry_cpu_function(self):
          pass
  </code></pre>
  
  <p><strong>Edit</strong> added models.py as it may be relevant for the entries since it's a different class.</p>
  
  <p>models.py </p>
  
  <pre><code>class Trade(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
      status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='op')
      broker = models.ForeignKey(Broker, on_delete=models.CASCADE, blank=True, null=True)
      asset = models.ForeignKey(Asset, on_delete=models.CASCADE, null=True)
      ...
  
  class Entry(models.Model):
  
      ENTRY = 'entry'
      EXIT = 'exit'
      ENTRY_TYPE_CHOICES = [
          (ENTRY, 'Entry'),
          (EXIT, 'Exit'),
  
      trade = models.ForeignKey(Trade, on_delete=models.CASCADE)
      amount = models.FloatField(null=True)
      price = models.FloatField(null=True, blank=True)
      entry_type = models.CharField(max_length=5, choices=ENTRY_TYPE_CHOICES, default=ENTRY)
      ...
  </code></pre>