Django PositiveIntegerField 接受否定

Django PositiveIntegerField accepting Negatives

我的 Django 应用程序中有一个带有 PositiveIntegerField 的模型 (views):

class Post(models.Model):
    author = models.ForeignKey('UserProfile')
    creation_date = models.DateTimeField(auto_now_add=True)
    views = models.PositiveIntegerField(default=0)
    tags = models.ManyToManyField('Tag', through="PostTagging", null=False, blank=False)
    rating = models.FloatField(default=0)

然而,当我测试它时,它接受负值:

测试:

def test_post_with_negative_views(self):
    test_user = User.objects.get(username='test_student')
    test_user_profile = UserProfile.objects.get(user=test_user)

    post = Post.objects.create(author=test_user_profile, title='Django Testing', content='hello world', views=-10)
    self.assertEquals(post.views, 0)

失败:

Creating test database for alias 'default' ...
......F.......
=====================================================================
FAIL: test_post_with_negative_views (bark.tets.PostTest)
---------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ewan/Documents/WAD2/studeso/bark/bark/tests.py", line 58, in test_post_with_negative_views
    self.assertEquals(post.views, 0)
AssertionError: -10 != 0

---------------------------------------------------------------------

FAILED (failures=1)

我是不是做错了什么?

我已经尝试使用 int(-10) 和 int("-10") 对其进行测试,以防它是一个字符串格式错误,我得到了很多。

catavaran 的回答包括:

post.full_clean()

同样失败。

以下是文档 validating objects 章节的摘录:

Note that full_clean() will not be called automatically when you call your model’s save() method. You’ll need to call it manually when you want to run one-step model validation for your own manually created models.

所以验证和保存模型应该是这样的:

post = Post(author=test_user_profile, title='Django Testing',
            content='hello world', views=-10)
post.full_clean()
post.save()

UPDATE:SQLite 后端似乎关闭了此验证。我在 django.db.backends.sqlite3.operations.DatabaseOperations class.

中找到了这段代码
def integer_field_range(self, internal_type):
    # SQLite doesn't enforce any integer constraints
    return (None, None)

此方法的值用于为 PositiveIntegerField.

构建验证器

据我了解,这样做是出于兼容性原因。所以如果你想使用 SQLite 那么你必须手动添加验证器:

from django.core.validators import MinValueValidator

class Post(models.Model):
    ...
    views = models.PositiveIntegerField(default=0,
                                        validators=[MinValueValidator(0)])

修改后 full_clean() 应该可以正常工作。