问题是我重写了BaseUser,以允许访问者使用电子邮件和密码(而不是Django默认用户名)登录。除非引入序列化程序,否则代码无法正常工作。 如果我将User对象传递给User Serializer,它将给出
AttributeError:“ NoneType”对象没有属性“ _meta”
下面是我的文件:
View.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模型
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)
运营商型号
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序列化程序
class CreateCarrierSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email','domainID','roleID','user_name','phone')
运营商串行器
class CarrierExtraInfoSerializer(serializers.ModelSerializer):
class Meta:
model = CarrierExtraInfo
exclude = ('created_DT','updated_DT')
输出错误
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
为什么我要这个? 迁移时没有错误,但是在POST请求中此错误到达。
答案 0 :(得分:3)
当你有一个自定义的 Django User 模型时,确保你在启动 User (From the Django Docs) 的文件中正确导入它。因此,将以下内容添加到 serializers.py 文件的顶部。这为我修复了同样的错误。
from django.contrib.auth import get_user_model
User = get_user_model()
答案 1 :(得分:1)
缩进不正确
class CreateCarrierSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('email','domainID','roleID','user_name','phone')
答案 2 :(得分:0)
您还必须更新导入,以便它们指向您的新自定义用户模型。