合并用户和配置文件时 signals.py 上的 AttributeError
AttributeError on signals.py when combine user and profile
在 signals.py 随附的注册模板表单上将用户与自定义配置文件组合后与用户模型同步。知识库提供了将 related_name 添加到 OnetoOneFiedld 上的 models.py 的解决方案,但问题仍然存在。
AttributeError at /createuser
'User' object has no attribute 'profile'
Request Method: POST
Request URL: http://127.0.0.1:8000/createuser
Django Version: 3.1.2
Exception Type: AttributeError
Exception Value:
'User' object has no attribute 'profile'
Exception Location: D:\Dev\main0\main\accounts\signals.py, line 16, in update_profile
Python Executable: D:\Dev\main0\venv\Scripts\python.exe
Python Version: 3.8.5
Python Path:
['D:\Dev\main0\main',
'D:\Dev\main0\venv\Scripts\python38.zip',
'c:\python38\DLLs',
'c:\python38\lib',
'c:\python38',
'D:\Dev\main0\venv',
'D:\Dev\main0\venv\lib\site-packages']
Server time: Sun, 01 Nov 2020 14:34:42 +000
我试图找出错误的根本原因
这是 Signals.py 文件
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created == False:
instance.profile.save()
和views.py
def CreateUser(request):
if request.method == 'POST':
first_name = request.POST['first_name']
last_name = request.POST['last_name']
username = request.POST['username']
password = request.POST['password1']
repassword = request.POST['password2']
email = request.POST['email']
if password == repassword:
if User.objects.filter(username=username).exists():
messages.error(request, f'User {username} already existed!!!')
return redirect('register-page')
elif User.objects.filter(email=email).exists():
messages.error(request, f'email {email} already existed!!!')
return redirect('register-page')
else:
user = User.objects.create_user(
username=username,
password=password,
email=email,
first_name=first_name,
last_name=last_name
)
user.save()
profile = Profile.objects.create(
gender=gender,
date_of_birth=date_of_birth,
phone_number=phone_number,
photo=photo
)
profile.user = user
profile.save()
messages.success(request, f'User {username} has been created successfully!!')
return redirect('main-users-page')
else:
messages.info(request,'password does not match')
return redirect('register-page')
else:
return render(request, 'registration/register.html')
在 signals.py
上添加详细信息的解决方案
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_profile, sender=User)
@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created == False:
instance.profile.save()
print('Profile updated!!')
post_save.connect(update_profile, sender=User)
在 signals.py 随附的注册模板表单上将用户与自定义配置文件组合后与用户模型同步。知识库提供了将 related_name 添加到 OnetoOneFiedld 上的 models.py 的解决方案,但问题仍然存在。
AttributeError at /createuser
'User' object has no attribute 'profile'
Request Method: POST
Request URL: http://127.0.0.1:8000/createuser
Django Version: 3.1.2
Exception Type: AttributeError
Exception Value:
'User' object has no attribute 'profile'
Exception Location: D:\Dev\main0\main\accounts\signals.py, line 16, in update_profile
Python Executable: D:\Dev\main0\venv\Scripts\python.exe
Python Version: 3.8.5
Python Path:
['D:\Dev\main0\main',
'D:\Dev\main0\venv\Scripts\python38.zip',
'c:\python38\DLLs',
'c:\python38\lib',
'c:\python38',
'D:\Dev\main0\venv',
'D:\Dev\main0\venv\lib\site-packages']
Server time: Sun, 01 Nov 2020 14:34:42 +000
我试图找出错误的根本原因 这是 Signals.py 文件
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created == False:
instance.profile.save()
和views.py
def CreateUser(request):
if request.method == 'POST':
first_name = request.POST['first_name']
last_name = request.POST['last_name']
username = request.POST['username']
password = request.POST['password1']
repassword = request.POST['password2']
email = request.POST['email']
if password == repassword:
if User.objects.filter(username=username).exists():
messages.error(request, f'User {username} already existed!!!')
return redirect('register-page')
elif User.objects.filter(email=email).exists():
messages.error(request, f'email {email} already existed!!!')
return redirect('register-page')
else:
user = User.objects.create_user(
username=username,
password=password,
email=email,
first_name=first_name,
last_name=last_name
)
user.save()
profile = Profile.objects.create(
gender=gender,
date_of_birth=date_of_birth,
phone_number=phone_number,
photo=photo
)
profile.user = user
profile.save()
messages.success(request, f'User {username} has been created successfully!!')
return redirect('main-users-page')
else:
messages.info(request,'password does not match')
return redirect('register-page')
else:
return render(request, 'registration/register.html')
在 signals.py
上添加详细信息的解决方案 @receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_profile, sender=User)
@receiver(post_save, sender=User)
def update_profile(sender, instance, created, **kwargs):
if created == False:
instance.profile.save()
print('Profile updated!!')
post_save.connect(update_profile, sender=User)