Django model to do accounting for a Litecoin miner pool I'm coming from Java so I am used to performing calculations I require in separate functions. I would like to know if the below code makes sense in my Django model especially the loop and calling functions from within functions.

The aim of this is to create a backend for multiple Litecoin miners (L3's). Every miner will contribute to the same pool but be plugged in at different times. Therefore each will get its own time stamp. Each month I will pull the total pool amount (pulled from api) and use the total amount of hours contributed to the pool to calculate the litecoins-to-hour ratio. This ratio I can then multiply to each miners' hours worked for the month to determine that particular miners Litecoin earned.

class L3(models.Model):
    user = models.ForeignKey(User, related_name="L3")
    MachineID = models.CharField(max_length=256)
    TimeOnline = models.DateTimeField()
    Commission = models.IntegerField(default='10')
    TimeOffline = models.IntegerField(default='0')

    def indivMachineHoursWorked(self):
        this_month = datetime.now().month
        if self.TimeOnline == this_month:
            x = datetime.now() - self.TimeOnline
        else:
            x = datetime.now() - datetime.today().replace(day=1) #need to update hour to 0 too i.e. start of month
        return x

    def totalMachineHoursWorked(self):
        L3_total = L3.objects.all()
        hrs=0
        for l3 in L3_total:
            hrs =+ l3.indivMachineHoursWorked()
        return hrs

    def btcToHour(self):
        main_api = 'https://api.nicehash.com/api?method=balance&amp;id=123456&amp;key=01234567-abcd-dcba-abcd-0123456789abc'
        url = main_api
        json_data = requests.get(url).json()
        print(json_data)
        balance1 = json_data['result']['balance_confirmed']
        print(balance1)
        btc2hour = balance1/self.totalMachineHoursWorked()
        return btc2hour

    def indivMachineTurnover(self):
        return self.indivMachineHoursWorked()*self.btcToHour()

    def __str__(self):
        return self.user.get_full_name()
totalMachineHoursWorked should be a @classmethod.  It could be written more succinctly like this, using sum():

@classmethod
def totalMachineHoursWorked(cls, self):
    return sum(l3.indivMachineHoursWorked() for l3 in cls.objects.all())


However, I have doubts that any of the calculations should be done in Python.  totalMachineHoursWorked() needs to fetch the entire table.  I would recommend trying to find a way to do all of these calculations in SQL instead, because that's what SQL servers are designed to do: provide an efficient tool to let you extract aggregate reports by formulating your query intelligently.

In particular, if you need to calculate btcToHour() for multiple hosts, that would be horribly inefficient, since it would need to calculate totalMachineHoursWorked() multiple times, each of which involves fetching the entire table, and also performing a calculation on an individual row.