python - 如何修复 'Meta.fields'不得包含非模型字段名称: username for django graphene authentication endpoint

标签 python python-3.x django graphene-python django-graphql-auth

在我的 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,
    )

我正在使用 Graphite 烯作为 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

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

最佳答案

这就是我解决这个问题的方法。

通过添加以下内容来修改应用设置文件,

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

关于python - 如何修复 'Meta.fields'不得包含非模型字段名称: username for django graphene authentication endpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70936957/

相关文章:

python - 使用python从sql查询中的文本文件调用日期

python - 获取 LinkedIn 公司股票 Python

Python "IOError: [Errno 21] Is a directory: '/home/thomasshera/Pictures/Star Wars'"

django - 重置 celery 任务的倒计时

python - 编辑 list 表格重复

python - 如何使用 Python 将此字符串转换为 iso 8601

python - 从 Foursquare API 解析嵌套的 JSON

python - 通过将凭据作为参数传递,使用 boto3 从 S3 Bucket 下载文件

python - 如何存储 TfidfVectorizer 以备将来在 scikit-learn 中使用?

python - Django 表单集重复值