Django看不到我的有效

2024-09-28 05:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用我自己定制的验证器。问题是,Django看不到验证器。你知道吗

你知道吗型号.py地址:

class Market(models.Model):
    money = models.DecimalField(max_digits = 15, decimal_places=2, validators=[moneyValidator])

D = decimal.Decimal

def moneyValidator(x):
    if not convStr(x):
        raise ValidationError
    if (x <= 0) or (x >= PAYOFF) or (D(str(x))%D('0.01') != 0):
        raise ValidationError

def convStr(x):
    try:
        str(x)
        return True
    except ValueError:
        return False

错误消息:

NameError: name 'moneyValidator' is not defined

我想是Django从任何地方调用这个函数,不知道在哪里可以找到这个函数。我能怎么办?你知道吗


Tags: ordjango函数returnifmodelsdefnot
1条回答
网友
1楼 · 发布于 2024-09-28 05:18:28

方法moneyValidator应该在类声明之前声明:

def moneyValidator(x):
    ...

class Market(models.Model):
    money = models.DecimalField(max_digits = 15, decimal_places=2, validators=[moneyValidator])

相关问题 更多 >

    热门问题