user4981459
user4981459

Reputation:

How to fix 'Meta.fields' must not contain non-model field names: username for django graphene authentication endpoint

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.

Upvotes: 2

Views: 3056

Answers (3)

Your custom user class "User" doesn't have "username" field so you need to remove "username" field from "GRAPHQL_AUTH" settings which are set "username" field by default.

So, by default, "username" field is set to "LOGIN_ALLOWED_FIELDS", "REGISTER_MUTATION_FIELDS" and "USER_NODE_FILTER_FIELDS" in "GRAPHQL_AUTH" settings as shown below:

GRAPHQL_AUTH = {                      # ↓ Here
    'LOGIN_ALLOWED_FIELDS': ["email", "username"],
    'REGISTER_MUTATION_FIELDS': ["email", "username"],
    'USER_NODE_FILTER_FIELDS': {          # ↑ Here
        "email": ["exact"],
        "username": ["exact", "icontains", "istartswith"], # ← Here
        "is_active": ["exact"],
        "status__archived": ["exact"],
        "status__verified": ["exact"],
        "status__secondary_email": ["exact"],
    }
}

So, to remove "username" field from them, you need to redefine them in "settings.py" as shown below:

# "settings.py"

GRAPHQL_AUTH = {
    'LOGIN_ALLOWED_FIELDS': ['email'],
    'REGISTER_MUTATION_FIELDS': ['email'],
    'USER_NODE_FILTER_FIELDS': { 
        "email": ["exact"], 
        "is_active": ["exact"],
        "status__archived": ["exact"],
        "status__verified": ["exact"],
        "status__secondary_email": ["exact"],
    }
}

Upvotes: 0

user4981459
user4981459

Reputation:

This is how I solved this issue.

Modify the app settings file by adding below,

from graphql_auth.settings import DEFAULTS

DEFAULTS['LOGIN_ALLOWED_FIELDS'] = ['email']
DEFAULTS['REGISTER_MUTATION_FIELDS'] = ['email']
DEFAULTS['USER_NODE_FILTER_FIELDS'] = {
    'email': ['exact'],
    'is_active': ['exact'],
    'status__archived': ['exact'],
    'status__verified': ['exact'],
    'status__secondary_email': ['exact'],
}
GRAPHQL_AUTH = DEFAULTS

Upvotes: 2

Ahtisham
Ahtisham

Reputation: 10116

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

Upvotes: 0

Related Questions