AssertionError:您需要在UserProfile.Meta中传递有效的Django模型,并收到& quot; None& quot;. [英] AssertionError: You need to pass a valid Django Model in UserProfile.Meta, received "None"

查看:49
本文介绍了AssertionError:您需要在UserProfile.Meta中传递有效的Django模型,并收到& quot; None& quot;.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm adding Django Model to a graphql api using the AbstractBaseUser custom user model. The Admin works fine except that I get an error when trying to access the graphql api, 'You need to pass a valid Django Model in UserProfile.Meta, received "None"'

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

I've tried adding AUTH_USER_MODEL = 'noxer_app.MyUser' to settings, yet it doesn't work

在models.py中:

In 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

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接口,但出现此错误:

I expect to see the Graphql api interface, but I get this error:

您需要在UserProfile.Meta中传递有效的Django模型,并收到无"信息.

You need to pass a valid Django Model in UserProfile.Meta, received "None".

推荐答案

我认为应该是 class Meta ,而不是 class meta .元以大写字母开头.这就是为什么它不认识您描述模型的原因.

I think it should be class Meta not class meta. The Meta starts with a capital. That's why it doesn't recognize that you describe the model.

这篇关于AssertionError:您需要在UserProfile.Meta中传递有效的Django模型,并收到& quot; None& quot;.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆