How to convert a models.IntegerField() instance to int?

I've read How to convert a models.IntegerField() to an integer(the poster actually need copy-constructor function).And I've searched for Google.

But it doesn't help.

what I want to do is:

#In app/models.py

class Foo:
    a1 = models.IntergeField()
    a2 = models.IntergeField()
    #....
    #many else
    b1 = convertToInt(a1) * 3 + convertToInt(a2) *4 + convertToInt(a7) #calc by needing
    b2 = convertToInt(a2) * 2 + convertToInt(a3) + convertToInt(a5) #calc by needing
    #....
    #many else
    #b(b is price actually) will be used in somewhere else.Its type need be int for programmer-friendly using

any advice?

P.S. English is not my first language.Please forgive my syntax mistakes.

Since you indicated in the comments that you don't need to store the derived attributes, I propose the following solution. Here the attributes are calculated every time and you can use them as you would use a1 and a2 attributes.


class Foo(models.Model):
    a1 = models.IntegerField()
    a2 = models.IntegerField()
 
    @property
    def b1(self):
        return self.a1*3 + self.a2*4  # + ...

    @property
    def b2(self):
        return self.a2*3 + self.a3  # + ...
Back to Top