简体   繁体   中英

Expected type 'int', got 'IntegerField' instead Django

I am trying to get an object of class Guide to be the value of a calculation of other objects in the class. This is how far I have got in my code but got error

Expected type 'int', got 'IntegerField' instead.

How do I get this to work?

Here is my code:

class Guide(models.Model): 
    guide_title = models.CharField(max_length=200) 
    guide_category = models.CharField(max_length=70) 
    guide_why = models.TextField() 
    guide_how = models.TextField() 
    user_current_conversion_rate = models.IntegerField(default=0) 
    user_optimal_conversion_rate = models.IntegerField(default=0) 
    user_monthly_visitors = models.IntegerField(default=0)
    user_average_order_value = models.IntegerField(default=0) 
    user_increase_conversion_rate = models.IntegerField(default=0)

    @property
    def user_increase_conversion_rate(self):
        return int(
           (self.user_monthly_visitors*(self.user_optimal_conversion_rate/100))*
            self.user_average_order_value) -
            ((self.user_monthly_visitors*(self.user_current_conversion_rate/100))*
            self.user_average_order_value)

If you need to do handling on a field via @property , you can change the field name to add an underscore (by convention) and then write property getter and setter methods. That way Django cleanly knows how to set the field, and you get control over how it gets set.

See:

https://www.stavros.io/posts/how-replace-django-model-field-property/

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