In my Django application I have created customer user model which uses email as username.
class UserManager(BaseUserManager):
"""Define a model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True,)
user_id = models.UUIDField(
default=uuid4,
unique=True,
)
I am using graphene for APIs. For authentication endpoint I am following below steps, https://django-graphql-auth.readthedocs.io/en/latest/quickstart/
I am keep getting below error,
sports_league-web-1 | File "/usr/local/lib/python3.10/site-packages/django_filters/filterset.py", line 71, in __new__
sports_league-web-1 | new_class.base_filters = new_class.get_filters()
sports_league-web-1 | File "/usr/local/lib/python3.10/site-packages/django_filters/filterset.py", line 358, in get_filters
sports_league-web-1 | raise TypeError(
sports_league-web-1 | TypeError: 'Meta.fields' must not contain non-model field names: username
Kindly advise what I am doing wrong here. Let me know if any other details needed.
CodePudding user response:
Just remove username field in your model as you have setup email as username:
class User(AbstractUser):
email = models.EmailField(_('email address'), unique=True,)
user_id = models.UUIDField(
default=uuid4,
unique=True,
)