Django如何关闭警告

时间:2015-03-23 20:09:25

标签: python django logging warnings messages

我有一个特殊的用户模型,拥有自己的身份验证后端。 Django照顾我并发送通知很好,但我怎么能关掉一些警告,如下:

WARNINGS:
profile.User: (auth.W004) 'User.email' is named as the 'USERNAME_FIELD', but it is not unique.
    HINT: Ensure that your authentication backend(s) can handle non-unique usernames.

我的用户模型:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'))
    site = models.ForeignKey(Site, verbose_name=_("Site"), null=True, blank=True)
    class Meta:
        unique_together = (
            ("email", "site", ),
        )

2 个答案:

答案 0 :(得分:13)

在查看我自己的项目的设置文档时,我偶然发现了一个让我想起你的问题的设置。

自Django 1.7以来,有一种设置可以使某些警告静音。如果您使用的是Django 1.7或更高版本,则可以将错误代码添加到SILENCED_SYSTEM_CHECKS设置:

# settings.py

SILENCED_SYSTEM_CHECKS = ["auth.W004"]

来源:https://docs.djangoproject.com/en/1.7/ref/settings/#silenced-system-checks

答案 1 :(得分:-1)

警告可以帮助您,因此通常最好改进您的代码以避免它们。

在这种情况下,你真的不想转过那个警告。如果您阅读警告,您会看到目前有两个不同的用户使用相同的用户名!

要解决此问题,您应该通过在字段定义中添加email来使unique=True字段唯一:

email = models.EmailField(unique=True)
相关问题