15

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", ),
        )
Arti
  • 7,356
  • 12
  • 57
  • 122

2 Answers2

34

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

jjkester
  • 685
  • 5
  • 6
  • There is a [list](https://docs.djangoproject.com/en/stable/ref/checks/) of other system check messages, but those anyone is likely interested have their specifier being output to the Django logs. – Kevin Feb 08 '22 at 20:57
-2

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)
jjkester
  • 685
  • 5
  • 6
  • 2
    No, my model have 2 unique fields: ```unique_together = ( ("email", "site", ), )``` So, i can't set ```email = models.EmailField(unique=True)``` – Arti Mar 24 '15 at 08:49
  • As the OP said, he wanted the (email + site) to be unique, meaning email cannot. – tito Dec 29 '18 at 11:05