Q:

Django Create Class From Multiple Functions

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. 
What is the proper way to turn this into a class? 
Is this even the right approach?

utils.py | multiple functions
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) > 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

utils.py | one class
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

Edit added models.py as it may be relevant for the entries since it's a different class.
models.py 
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)
    ...

A:

You can move all of these functions under the model or custom QuerySet.
If you move to the model you can do something similar. (Code might not work, just given for your reference)
# MODEL
class Trade(models.Model):

    # Model fields here

    def get_entries(self):
        # NB: assuming you didn't specify a related_name
        # in the `Entry.trade` ForeignKey field. Else,
        # replace `entry_set` with said related_name.
        return self.entry_set.filter(entry_type="entry")

    def max_amount_function(self):
        max_amount_res = self.get_entries().aggregate(max_amount_function=Sum('amount'))['max_amount_function']
        if max_amount_res is None:
            return 0
        else:
            return max_amount_res

    def fees_function(self):
        return self.get_entries().aggregate(fees_function=Sum('fee'))['fees_function']

    def entry_cpu_function(self, ):

        if self.max_amount_function() <= 0:
            return 0

        entry_cpu_function = self.get_entries().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

Inside views.py, you can refer this functions like below - 
trade = get_object_or_404(Trade, pk=pk)
trade.max_amount_function()
trade.fees_function()
trade.entry_cpu_function()

