简体   繁体   中英

Django how to turn off warning

I have a special user model, with own auth backend. It's good that Django take care about me and send notifications, but how i can turn off some warnings, like this:

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.

My user model:

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", ),
        )

While looking into the settings documentation for a project of my own I stumbled upon a setting that reminded me of your question.

Since Django 1.7 there is a setting to silence certain warnings. If you are using Django 1.7 or later you can add the error code to the SILENCED_SYSTEM_CHECKS setting:

# settings.py

SILENCED_SYSTEM_CHECKS = ["auth.W004"]

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

Warnings are there to help you, so mostly it is best to improve your code to avoid them.

In this case you really do not want to turn of that warning. If you read the warning, you see that currently there can be two different users with the same username!

To solve this, you should make the email field unique by adding unique=True to the field definition:

email = models.EmailField(unique=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM