如何修复 'Meta.fields' 不得包含非模型字段名称:django 石墨烯身份验证端点的用户名

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

在我的 Django 应用程序中,我创建了使用电子邮件作为用户名的客户用户模型。

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

我正在将石墨烯用于 API。对于身份验证端点,我遵循以下步骤, https://django-graphql-auth.readthedocs.io/en/latest/quickstart/

我一直低于错误,

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

请指教我这里做错了什么。如果需要任何其他详细信息,请告诉我。

只需删除模型中的用户名字段,因为您已将电子邮件设置为用户名:

class User(AbstractUser):
    email = models.EmailField(_('email address'), unique=True,)

    user_id = models.UUIDField(
        default=uuid4,
        unique=True,
   )

我就是这样解决这个问题的。

通过在下面添加修改应用程序设置文件,

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

您的自定义用户 class“用户” 没有 “用户名”字段 因此您需要删除 “用户名”字段来自“GRAPHQL_AUTH”设置,由“用户名”字段设置默认。

因此,默认情况下,“用户名”字段“GRAPHQL_AUTH”设置中设置为"LOGIN_ALLOWED_FIELDS", "REGISTER_MUTATION_FIELDS" and "USER_NODE_FILTER_FIELDS" ] 如下图:

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"],
    }
}

因此,要从中删除 “用户名”字段,您需要在 "settings.py" 中将它们重新定义为如下所示:

# "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"],
    }
}