无法将计算属性保存到django中的数据模型抛出错误'int'对象不可调用

问题描述 投票:0回答:1
class Room(models.Model):

a = models.PositiveIntegerField(default=0, validators=[MaxValueValidator(9)],blank=True)
b = models.PositiveIntegerField(default=0, validators=[MaxValueValidator(99999)],blank=True)
c = models.PositiveIntegerField(default=0, validators=[MaxValueValidator(9999)],blank=True)


@property
def cal_room_rent(self):
    if self.room_type == 'SS' :
        return 14000
    elif self.room_type == 'DS' :
        return 8000
    elif self.room_type == 'TS' :
        return 6000

@property
def cal_security_deposit(self):
    return 3000


@property
def cal_limit(self):
    if self.room_type == 'SS' :
        return 1
    elif self.room_type == 'DS' :
        return 2
    elif self.room_type == 'TS' :
        return 3

def save(self, *args, **kwargs):
    self.a=self.cal_limit()
    self.b=self.cal_room_rent()
    self.c=self.security_deposit()
    super(Room, self).save(*args, **kwargs)

OUTPUT:

TypeError at /admin/pgmanagement/room/add/

'int' object is not callable

Request Method:     POST
Request URL:    http://127.0.0.1:8000/admin/pgmanagement/room/add/
Django Version:     2.1.4
Exception Type:     TypeError
Exception Value:    

'int' object is not callable

Exception Location:     E:\anil_website\svcomforts_1\pgmanagement\models.py in save, line 93
django python-3.7
1个回答
1
投票

你有这个错误,因为cal_limitcal_room_rentsecurity_deposit返回一个int对象,你试图调用它。试试这个:

def save(self, *args, **kwargs):
    self.a=self.cal_limit
    self.b=self.cal_room_rent
    self.c=self.security_deposit
    super(Room, self).save(*args, **kwargs)
© www.soinside.com 2019 - 2024. All rights reserved.