Profile() 在扩展 Django 的用户模型时得到了一个意外的关键字参数 'user'
Profile() got an unexpected keyword argument 'user' while extending django's User model
这里我用配置文件模型扩展了 django 的用户模型,我想将这个字段添加到用户模型中。
以下是文件。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user_ref = models.OneToOneField(User, on_delete=models.CASCADE)
pr1_points = models.IntegerField(default=0)
pr2_points = models.IntegerField(default=0)
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
user = instance
if created:
profile = Profile(user=user)
profile.save()
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
在views.py中的注册函数如下:
'''
def postregister(request):
if request.POST:
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
username = request.POST.get('username')
password1 = request.POST.get('password1')
password2 = request.POST.get('password2')
if password1 == password2:
if User.objects.filter(username=username).exists():
messages.error(request, 'Username is Taken Please Try Again')
return render(request,'signup.html')
elif User.objects.filter(email=email).exists():
messages.error(request, 'Email is Taken Please Try Again')
return render(request,'signup.html')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password1)
user.save()
print('You are registered successfully, Please sign to continue.')
return redirect('login')
else:
messages.error(request, 'Password is not matching Please Try Again')
return render(request,'signup.html')
else:
return redirect('register')
'''
因此在创建新用户时出现错误:Profile() 得到了一个意外的关键字参数 'user'。
所以请任何知道答案的人帮助我。
错误截图如下,供参考
[1]: https://i.stack.imgur.com/5qE4q.png
您在 Profile
模型中有 user_ref
。
替换
profile = Profile(user=user)
与
profile = Profile(user_ref=user)
这里我用配置文件模型扩展了 django 的用户模型,我想将这个字段添加到用户模型中。 以下是文件。
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user_ref = models.OneToOneField(User, on_delete=models.CASCADE)
pr1_points = models.IntegerField(default=0)
pr2_points = models.IntegerField(default=0)
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
user = instance
if created:
profile = Profile(user=user)
profile.save()
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
在views.py中的注册函数如下:
'''
def postregister(request):
if request.POST:
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
username = request.POST.get('username')
password1 = request.POST.get('password1')
password2 = request.POST.get('password2')
if password1 == password2:
if User.objects.filter(username=username).exists():
messages.error(request, 'Username is Taken Please Try Again')
return render(request,'signup.html')
elif User.objects.filter(email=email).exists():
messages.error(request, 'Email is Taken Please Try Again')
return render(request,'signup.html')
else:
user = User.objects.create_user(username=username, first_name=first_name, last_name=last_name, email=email, password=password1)
user.save()
print('You are registered successfully, Please sign to continue.')
return redirect('login')
else:
messages.error(request, 'Password is not matching Please Try Again')
return render(request,'signup.html')
else:
return redirect('register')
'''
因此在创建新用户时出现错误:Profile() 得到了一个意外的关键字参数 'user'。 所以请任何知道答案的人帮助我。
错误截图如下,供参考 [1]: https://i.stack.imgur.com/5qE4q.png
您在 Profile
模型中有 user_ref
。
替换
profile = Profile(user=user)
与
profile = Profile(user_ref=user)