简体   繁体   English

AttributeError : 'NoneType' 对象没有属性 '_meta'

[英]AttributeError : 'NoneType' object has no attribute '_meta'

The issue is I overridded the BaseUser to allow visitors to login with Email and Password instead of the Django default Username.问题是我覆盖了 BaseUser 以允许访问者使用电子邮件和密码而不是 Django 默认用户名登录。 Code works fine unless we introduce serializer.除非我们引入序列化程序,否则代码工作正常。 If I pass a User object to the User Serializer it gives如果我将 User 对象传递给 User Serializer,它会给出

AttributeError : 'NoneType' object has no attribute '_meta' AttributeError : 'NoneType' 对象没有属性 '_meta'

Below are my files:以下是我的文件:

View.py查看.py

class Carrier(APIView):
def post(self, request, format='json'):      #create
    try:
        data = request.data
        print(data['domainID'])
        domain = Domain.objects.get(id = data['domainID'])
        print(domain)
        role   = UserRole.objects.get(id = data['roleID'])
        print(role)

        user_obj = User(
                                                        email       = data['email'],
                                                        domainID    = domain,
                                                        roleID      = role,
                                                        user_name   = data['user_name'],
                                                        phone       = data['phone']
                                                )  

        user_obj.set_password(data['password'])                                              
        user_obj.save()

        carr_obj = CarrierExtraInfo.objects.create(CustomUserID=user_obj,somevalue=data['CustomUserID']['somevalue'])        
        context = {
            "data": data['user_name'],
            "message": 'verify your account'
        }
        carr_obj.save()
        return Response(context, status=status.HTTP_200_OK)
    except Exception as e:
        context = {
            "data": str(e),
            "message": 'invalid details'
        }
        return Response(context,status=status.HTTP_400_BAD_REQUEST)

custom_user model custom_user 模型

class User(AbstractBaseUser):
    objects = UserManager()
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )

    domainID    =  models.ForeignKey(Domain, on_delete = models.CASCADE, null=True,blank=True)
    roleID      =  models.ForeignKey(UserRole, on_delete = models.CASCADE, null=True,blank=True)
    user_name   =  models.CharField(max_length = 200,null=True,blank=True) 
    phone       =  models.CharField(max_length = 50,null=True,blank=True)
    is_verified =  models.BooleanField(default=False)
    active      =  models.BooleanField(default=True)
    staff       =  models.BooleanField(default=False) # a admin user; non super-user
    admin       =  models.BooleanField(default=False) # a superuser
    created_DT  =  models.DateTimeField(auto_now_add=True)
    updated_DT  =  models.DateTimeField(auto_now=True)

carrier model载体模型

class CarrierExtraInfo(models.Model):
    CustomUserID  = models.OneToOneField(User,on_delete=models.CASCADE,null=True,blank=True)
    somevalue     = models.CharField(max_length=100,null=True,blank=True)
    is_active     = models.BooleanField(default=True)


    created_DT    = models.DateTimeField(auto_now_add=True)
    updated_DT    = models.DateTimeField(auto_now=True)

custom_user serializer custom_user 序列化程序

class CreateCarrierSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email','domainID','roleID','user_name','phone')

carrier serializer载波串行器

class CarrierExtraInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = CarrierExtraInfo
        exclude = ('created_DT','updated_DT')

output error输出错误

File "E:\KargoLogic\venv\lib\site-packages\rest_framework\serializers.py", line 370, in _writable_fields
  field for field in self.fields.values() if not field.read_only 
File "E:\KargoLogic\venv\lib\site-packages\rest_framework\serializers.py", line 363, in fields
  for key, value in self.get_fields().items(): 
File "E:\KargoLogic\venv\lib\site-packages\rest_framework\serializers.py", line 1024, in get_fields
  info = model_meta.get_field_info(model) 
File "E:\KargoLogic\venv\lib\site-packages\rest_framework\utils\model_meta.py", line 39, in get_field_info
  forward_relations = _get_forward_relationships(opts) 
File "E:\KargoLogic\venv\lib\site-packages\rest_framework\utils\model_meta.py", line 96, in _get_forward_relationships
  not field.remote_field.through._meta.auto_created
AttributeError: 'NoneType' object has no attribute '_meta'

"POST /carr/api/carr_user HTTP/1.1" 500 16718

why i'm getting this?为什么我得到这个? On migration there is no error, but on POST request this error arrives.迁移时没有错误,但在 POST 请求时出现此错误。

When you have a custom Django User model make sure you are importing it correctly in files that initiate User ( From the Django Docs ).当你有一个自定义的 Django User 模型时,确保你在启动 User 的文件中正确导入它( 来自 Django 文档)。 So, add the following to the top of your serializers.py file.因此,将以下内容添加到 serializers.py 文件的顶部。 This fixed the same error for me.这为我修复了同样的错误。

from django.contrib.auth import get_user_model

User = get_user_model()

Not Indented Properly没有正确缩进

class CreateCarrierSerializer(serializers.ModelSerializer):
     class Meta:
        model = User
        fields = ('email','domainID','roleID','user_name','phone')

您还必须更新导入,以便它们指向您的新自定义 User 模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Rest Framework:AttributeError:'NoneType'对象没有属性'_meta'[for OneToOneField] - Django Rest Framework: AttributeError: 'NoneType' object has no attribute '_meta' [for OneToOneField] django-factory_boy AttributeError:“ NoneType”对象没有属性“ _meta” - django - factory_boy AttributeError: 'NoneType' object has no attribute '_meta' Django CMS-AttributeError:“ NoneType”对象没有属性“ _meta” - Django CMS - AttributeError: 'NoneType' object has no attribute '_meta' django,python:AttributeError: 'NoneType' 对象没有属性 '_meta' - django,python:AttributeError: 'NoneType' object has no attribute '_meta' AttributeError: 'NoneType' 对象没有属性 - AttributeError: 'NoneType' object has no attribute AttributeError:“ NoneType”对象没有属性“ a” - AttributeError: 'NoneType' object has no attribute 'a' “ NoneType”对象没有属性“ META” - 'NoneType' object has no attribute 'META' 使用加密的 SearchField 作为自定义用户 model (Django) 的密钥时出错:AttributeError: 'NoneType' object 没有属性 '_meta' - Error when using encrypted SearchField as key for custom user model (Django): AttributeError: 'NoneType' object has no attribute '_meta' AttributeError:'NoneType'对象没有属性'mention' - AttributeError: 'NoneType' object has no attribute 'mention' Python,AttributeError:“ NoneType”对象没有属性“ show” - Python, AttributeError: 'NoneType' object has no attribute 'show'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM