TypeError:'RegexValidator'对象不可迭代

时间:2017-11-02 06:10:09

标签: django django-models django-rest-framework django-validation

我正在构建颜色的整数数组字段。

我尝试使用CommaSeparatedIntegerField,但它已被删除

CommaSeparatedIntegerField has been deprecated. 
Support for it (except in historical migrations) will be removed in Django 2.0.
    HINT: Use CharField(validators=[validate_comma_separated_integer_list]) instead

所以我使用了将颜色字段设置为CharField而不是CommaSeparatedIntegerField作为推荐

from django.core.validators import validate_comma_separated_integer_list

class Cloth(models.Model):
    color = models.CharField(validators=validate_comma_separated_integer_list)

但是我在制作游戏时遇到了这个错误

TypeError: 'RegexValidator' object is not iterable

为什么我收到此错误?我遵循了确切的准则:(

1 个答案:

答案 0 :(得分:5)

首先CharField需要max_length字段,而validators需要list []

所以,

class Cloth(models.Model):
    color = models.CharField(validators=[validate_comma_separated_integer_list],max_length=100)