将验证器添加到Django models.py中的字段

时间:2023-01-12 19:34:39

I am using django.core.validators inside my models.py but I keep getting syntax error.

我在models.py中使用django.core.validators但是我一直收到语法错误。

The piece of code involved is the following:

涉及的代码如下:

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator

class StepStart(models.Model):
    motor_type_choices = (                                                  
    ('1'),
    ('2'),
    ('3'),
    ('4'),
    ('5'),
    ('6'),
    )
    temp_start = models.IntegerField(                                       
        default=30,
        validators=[MaxValueValidator(90), MinValueValidator(10)]
    )
    level_start = models.IntegerField(                                     
        default=30,
        validators=[MaxValueValidator(150), MinValueValidator(10)]
    )

The error is a syntax error on the line of default=30.

该错误是默认行= 30的语法错误。

I've checked on other posts here and the validation method I used seems okay.

我在这里检查了其他帖子,我使用的验证方法似乎没问题。

Can someone point out the error please?

有人可以指出错误吗?

This is the error message I get in the command prompt:

这是我在命令提示符中得到的错误消息:

line 65
default=30,
      ^
SyntaxError: invalid syntax

Thanks.

1 个解决方案

#1


0  

change this:

temp_start = models.IntegerField(                                       
        default=30
        validators=[MaxValueValidator(90), MinValueValidator(10)]
    )
    level_start = models.IntegerField(                                     
        default=30
        validators=[MaxValueValidator(150), MinValueValidator(10)]
    )

to this:

temp_start = models.IntegerField(                                       
        default=30,
        validators=[MaxValueValidator(90), MinValueValidator(10)]
    )
    level_start = models.IntegerField(                                     
        default=30,
        validators=[MaxValueValidator(150), MinValueValidator(10)]
    )

#1


0  

change this:

temp_start = models.IntegerField(                                       
        default=30
        validators=[MaxValueValidator(90), MinValueValidator(10)]
    )
    level_start = models.IntegerField(                                     
        default=30
        validators=[MaxValueValidator(150), MinValueValidator(10)]
    )

to this:

temp_start = models.IntegerField(                                       
        default=30,
        validators=[MaxValueValidator(90), MinValueValidator(10)]
    )
    level_start = models.IntegerField(                                     
        default=30,
        validators=[MaxValueValidator(150), MinValueValidator(10)]
    )