简体   繁体   中英

TypeError: unsupported operand type(s) for *: 'PositiveIntegerField' and 'int'

Hello I am having a problem which I guess is really simple. I have the following class:

class Plant(models.Model):
    nominal_power = models.PositiveIntegerField()
    module_nominal_power= models.PositiveIntegerField()

    def calculation_of_components(a, b):
        return int((a*1000)/b)

    no_modules=calculation_of_components(nominal_power,module_nominal_power)

and I get the error: TypeError: unsupported operand type(s) for *: 'PositiveIntegerField' and 'int'

How can I solve this problem?

The problem is you're calling calculation_of_components at the creation time of the model class, when the fields have yet to take any value.

You can solve this by making no_modules a property so calculation_of_components does not get called at the creation of the model class, when the fields have no value:

class Plant(models.Model):
    nominal_power = models.PositiveIntegerField()
    module_nominal_power = models.PositiveIntegerField()

    def calculation_of_components(self, a, b):
        return int((a*1000)/b)

    @property
    def no_modules(self):
        return self.calculation_of_components(self.nominal_power, self.module_nominal_power)

You can then access no_modules like a regular model field:

plnt = Plant(...)
plnt.no_modules 

Pro-Tip: you can use integer division // in your calculation and avoid calling int : a * 1000 // b

First: calculation_of_components is a static method of class.

In your code no_modules is a result of function calculation_of_components . Probably You need a function:

class Plant(models.Model):
    nominal_power = models.PositiveIntegerField()
    module_nominal_power= models.PositiveIntegerField()

    @staticmethod    
    def calculation_of_components(a, b):
        return int((a*1000)/b)

    def no_modules(self):
        return self.calculation_of_components(self.nominal_power, self.module_nominal_power)

This error means the type of objects you're trying to multiply (*) are different objects, you cannot multiply PositiveIntegerField with int . You mixed PositiveIntegerField object with int object. You can make PositiveIntegerField appears in multiplication expressions by defining __mul__ operator overloading method in your class so when an instance of PositiveIntegerField appears with multiplication expression Python automatically overloads __mul__ method. In python 2.X __coerce__ gets called when different types of objects appear in such expressions in order to coerce them to a common type. Though, the use of __coerce__ isn't recommended.

Some classes that may be used in mathematical operations use __int__ to return an integer representing their values when needed:

class Num:
    def __int__(self):
        return self.value

int(Num()) * 20 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM