Kenneth Githambo
Kenneth Githambo

Reputation: 177

How to Implement django model methods

I am trying to build a system where we run depreciation on all assets in the database. Asset models is defined as below:

class Asset(models.Model):  
    Asset_description=models.TextField(max_length=100)
    Tag_number=models.TextField(unique=True)
    Asset_Cost=models.IntegerField(default=0)
    Monthly_Depreciation=models.IntegerField(default=0)
    Current_Cost=models.IntegerField(default=0) 

    def __str__(self):
        return self.Asset_description

How can i implement the depreciation formula in the models, e.g. Monthly_Depreciation=Asset_Cost/3/12 and Current_Cost=Asset_Cost-Monthly_Depreciation?

Upvotes: 0

Views: 1397

Answers (3)

Vladimir
Vladimir

Reputation: 10493

First, you need to distinguish between calculating Monthly_Depreciation and Current_Cost values in the python code, and having them in your database.

Calculate them in the code request to add corresponding methods like:

class Asset(models.Model):  
    Asset_description=models.TextField(max_length=100)
    Tag_number=models.TextField(unique=True)
    Asset_Cost=models.IntegerField(default=0)
    Monthly_Depreciation=models.IntegerField(default=0)
    Current_Cost=models.IntegerField(default=0) 

    def __str__(self):
        return self.Asset_description

    def get_monthly_deprecation(self):
        return self.Asset_Cost / 3.0 / 12.0

    def get_current_cost(self):
        return self.Asset_Cost - self.Monthly_Depreciation

And then you should be able to call them:

asset: Asset = ...
monthly_deprecation = asset.get_monthly_deprecation()
current_cost = asset.get_current_cost()

However that would not update the values in the database. To update you want to explicitly change the fields of the model and save the model then. This could be wrapped into the method of the Asset class:

class Asset(models.Model):
   ...
   def update_costs(self):
       self.Monthly_Depreciation = self.get_monthly_deprecation()
       self.Current_Cost = self.get_current_cost()
       self.save()  # This method will execute the actual UPDATE SQL statement

After calling this update_costs you will get the entry in the database updated:

asset: Asset = ...
asset.update_costs()
# Now both the model and the database should contain
# the calculated values, so you could use them as:
asset.Monthly_Deprecation
asset.Current_Cost

Upvotes: 1

quqa123
quqa123

Reputation: 675

model apart from being database instance also behaves like a standard python object so you simply define methods inside the class. In your scheme there is defined monthly depreciation as a attribute of your model but if it's dependant on other field it's better to define a simple method that returns the needed value for example:

class Asset(models.Model):  
    Asset_description=models.TextField(max_length=100)
    Tag_number=models.TextField(unique=True)
    Asset_Cost=models.IntegerField(default=0)
    # you don't need it anymore 
    # btw if you divide somthing by 3/12 it's better to use FloatField
    # Monthly_Depreciation=models.IntegerField(default=0)
    Current_Cost=models.IntegerField(default=0)

    def get_monthly_depreciation(self):
        return self.Asset_description/3/12

    def __str__(self):
        return self.Asset_description

just call the method whenever you need it. And remember that convention suggests using lowercase on attributes to improve your python knowleage read the link

Upvotes: 0

Sanil Khurana
Sanil Khurana

Reputation: 1169

You can do so by just creating the method in the model class.

class Asset(models.Model):  
    Asset_description=models.TextField(max_length=100)
    Tag_number=models.TextField(unique=True)
    Asset_Cost=models.IntegerField(default=0)
    Monthly_Depreciation=models.IntegerField(default=0)
    Current_Cost=models.IntegerField(default=0) 

    def __str__(self):
        return self.Asset_description

    def get_depreceation(self):
        depreceation = # Your formula here. You can use any the model fields using self
        return depreceation

Model classes can have normal methods and should have methods operating on that model. You can use this by just calling the method like you normally would for any python object

my_asset.get_depreceation()

Upvotes: 1

Related Questions