在django中IntegerField中的schemaVigration causeType为MaxValueValidator

时间:2012-11-28 07:49:37

标签: python django django-south typeerror

我在我的模特身上尝试schemamigration (我正在使用django 1.4 and python 2.6 on ubuntu

最初我的模特是

class Review(models.Model):
    reviewdate=models.DateField(default=date.today)
    description=models.TextField()
    author=models.ForeignKey(User,null=True)

我添加了一个字段

from django.core.validators import MinValueValidator,MaxValueValidator

class Review(models.Model):
    reviewdate=models.DateField(default=date.today)
    description=models.TextField()
    author=models.ForeignKey(User,null=True)
    rating = models.IntegerField(MinValueValidator(1),MaxValueValidator(10),default=5,help_text='integers 1 to 10')

当我跑

python manage.py schemamigration myapp --auto

我得到了这个TypeError

...
File "/home/me/Django-1.4/django/db/models/base.py", line 198, in __new__
    new_class._prepare()
  File "/home/me/Django-1.4/django/db/models/base.py", line 255, in _prepare
    cls.__doc__ = "%s(%s)" % (cls.__name__, ", ".join([f.attname for f in opts.fields]))
TypeError: Error when calling the metaclass bases
    sequence item 6: expected string, MaxValueValidator found

有人可以告诉我哪里错了吗?

1 个答案:

答案 0 :(得分:1)

您的语法不正确。您应该将验证器作为validators参数的列表提供:

models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(10)], default=5, help_text='integers 1 to 10')

有关详细信息,请参阅using validation in practice的文档。