微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django Rest Framework - 扩展用户模型 个人资料模型

如何解决django Rest Framework - 扩展用户模型 个人资料模型

晕,我在使用 django-restframework 通过 OnetoOnfield 扩展用户模型时遇到问题,下面是我的代码错误消息,我收到 TypeError:_create_user() 得到了一个意外的关键字参数“school_name” shool 当我尝试创建用户

###profile-serializer

class schoolProfileSerializer(serializers.ModelSerializer):
  id = serializers.IntegerField(source='pk',read_only=True)
  parser_classes = (MultiPartParser,FormParser,)
  email = serializers.CharField(source='user.email',read_only=True)
  username = serializers.CharField(source='user.username',read_only=True)
  badge = Base64Imagefield(max_length=None,use_url=True)
  date_established = serializers.DateField(format=None,input_formats=None)
  

  class Meta:
    model = Profile
    fields = ( 'email','id','username','school_name','address','badge','gender','level','motto')


  def create(self,validated_data):
    if 'profile' in validated_data:
      user_data = validated_data.pop('profile')
    user = CustomUser.objects._create_user(**validated_data)
    Profile.objects.update_or_create(user=user,**validated_data)
    return user

###profile-view

class CreateProfileView(generics.CreateAPIView):
  parser_classes = (MultiPartParser,)
  serializer_class = schoolProfileSerializer
  queryset = Profile.objects.all()
  permission_classes = [permissions.AllowAny]

个人资料模型

class Profile(models.Model):
  user = models.OnetoOneField(CustomUser,on_delete=models.CASCADE)
  school_name = models.CharField(max_length=255)
  address = models.TextField()
  school_phone_number = models.CharField(max_length=25)
  school_email = models.EmailField()
  motto = models.CharField(max_length=255)


@receiver(post_save,sender=CustomUser)
def create_school_profile(sender,instance=None,created=False,**kwargs):
  if created:
    Profile.objects.get_or_create(user=instance)

errorr-msg

Traceback (most recent call last):
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/django/core/handlers/exception.py",line 47,in inner
    response = get_response(request)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/django/core/handlers/base.py",line 179,in _get_response
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
    return view_func(*args,**kwargs)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/django/views/generic/base.py",line 70,in view
    return self.dispatch(request,*args,**kwargs)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/views.py",line 509,in dispatch
    response = self.handle_exception(exc)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/views.py",line 469,in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/views.py",line 480,in raise_uncaught_exception
    raise exc
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/views.py",line 506,in dispatch
    response = handler(request,**kwargs)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/generics.py",line 190,in post
    return self.create(request,**kwargs)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/mixins.py",line 19,in create
    self.perform_create(serializer)
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/mixins.py",line 24,in perform_create
    serializer.save()
  File "/home/olaneat/Desktop/myFiles/project/django/api/lib/python3.8/site-packages/rest_framework/serializers.py",line 205,in save
    self.instance = self.create(validated_data)
  File "/home/olaneat/Desktop/myFiles/project/django/api/schMartket/schProfile/serializers.py",line 60,in create
    user = CustomUser.objects._create_user(**validated_data)
TypeError: _create_user() got an unexpected keyword argument 'school_name'
[25/Mar/2021 13:29:50] "POST /school-profile/create HTTP/1.1" 500 124756

kinda confused but what am i doing wrongly can anyone help out

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。