AssertionError: You need to pass a valid Django Model in UserProfile.Meta, received "None"
AssertionError: You need to pass a valid Django Model in UserProfile.Meta, received "None"
我正在使用 AbstractBaseUser 自定义用户模型将 Django 模型添加到 graphql api。管理员工作正常,只是在尝试访问 graphql api、'You need to pass a valid Django Model in UserProfile.Meta, received "None"'
时出现错误
我试过将 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
app.schema.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
而不是class meta
。 Meta 以大写字母开头。这就是为什么它无法识别您描述的模型。
我正在使用 AbstractBaseUser 自定义用户模型将 Django 模型添加到 graphql api。管理员工作正常,只是在尝试访问 graphql api、'You need to pass a valid Django Model in UserProfile.Meta, received "None"'
时出现错误我试过将 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
app.schema.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
而不是class meta
。 Meta 以大写字母开头。这就是为什么它无法识别您描述的模型。