David__
David__

Reputation: 375

'Module' object is not callable

I am new to python and django and having trouble doing a math function in my model.py file.

class Orders(models.Model):
  ...
  total = models.DecimalField(
                              max_digits = 6,
                              decimal_places = 2,
                              null = True,
                              blank = True,
                              )
  ...  


  def shipping(self):
      t = self.total
      ship_rate = 0.12
      return(t*ship_rate)

When I call it in a python shell

dat = Orders.object.get(pk=12)
dat.shipping()

Then I get the following error message:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\xx\xx\models.py", line 613, in shipping  
ship_rate = 0.12  
TypeError: 'module' object is not callable  

Can anyone see what I am doing wrong?

Upvotes: 1

Views: 14047

Answers (3)

Sven Marnach
Sven Marnach

Reputation: 601669

The error is because you used

ship_rate = decimal(0.12)

This should be

ship_rate = decimal.Decimal(0.12)

decimal is the name of the module. You can't call a module, that's what the error message says. The reason for the strange traceback you got is that the module source and the code in memory got out of sync. When the traceback is created, the current version of the file is used, which might not be the version of the code that is actually running. Always reload your webserver to ensure it is using the most recent version of your code.

Upvotes: 8

tback
tback

Reputation: 11561

Looks like a typo: The manager is called objects.

Upvotes: 2

dm03514
dm03514

Reputation: 55962

You have to instantiate the class order = orders() then you can call order.shipping()

python class convention is also capital Class names Order and django conventions is singular name so it could be Order instead of orders

Upvotes: 0

Related Questions