0

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"'

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

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

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

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

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

4

1 回答 1

6

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

于 2019-09-17T13:52:02.293 回答