AssertionError:您需要在 UserProfile.Meta 中传递有效的 Django 模型,收到“无”

2024-01-16

我使用 AbstractBaseUser 自定义用户模型将 Django 模型添加到 graphql api。管理员工作正常,只是在尝试访问 graphql api 时出现错误,“您需要在 UserProfile.Meta 中传递有效的 Django 模型,收到“无””

我尝试将 AUTH_USER_MODEL = 'noxer_app.MyUser' 添加到设置中,但它不起作用

在 models.py 中:

from django.db import models
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)


class MyUserManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, company, company_reg_no, address, phone, image, password=None):

        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
            company=company,
            company_reg_no=company_reg_no,
            address=address,
            phone=phone,
            image=image,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, first_name, last_name, company, company_reg_no, address, phone, image, password):

        user = self.create_user(
            email,
            password=password,
            first_name=first_name,
            last_name=last_name,
            company=company,
            company_reg_no=company_reg_no,
            address=address,
            phone=phone,
            image=image,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    first_name = models.CharField(max_length=255, default='')
    last_name = models.CharField(max_length=255, default='')
    company = models.CharField(max_length=255, default='')
    company_reg_no = models.CharField(max_length=255, default='')
    address = models.CharField(max_length=400, default='')
    phone = models.CharField(max_length=13, default='')
    image = models.ImageField(default='noimage.jpg', upload_to = 'profile_pics')
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'company', 'company_reg_no', 'address', 'phone', 'image']

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin

应用程序架构.py

import graphene
from graphene import relay, ObjectType
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from django.contrib.auth.models import AbstractUser

from django.contrib.auth import get_user_model
from noxer_app.models import MyUser

class UserProfile(DjangoObjectType):
    class meta:

        model = MyUser

class Query(graphene.ObjectType):
    all_users = graphene.List(UserProfile)

    def resolve_all_users(self, info, **kwargs):
        return MyUser.objects.all()

我希望看到 Graphql api 接口,但收到此错误:

您需要在 UserProfile.Meta 中传递有效的 Django 模型,收到“None”。


我认为应该是class Meta not class meta。 Meta 以大写字母开头。这就是为什么它无法识别您描述的模型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AssertionError:您需要在 UserProfile.Meta 中传递有效的 Django 模型,收到“无” 的相关文章

随机推荐