我是Django的新手,并使用帐户系统创建Django项目。我用一对一的UserProfile模型扩展了User模型。
但是,我在将数据保存到UserProfile模型时遇到了问题(保存到User模型没有问题)。我尝试按照此guide进行修复,但在/ account / login /下遇到了 AttributeError “用户”对象没有属性“用户配置文件” 错误,这表示在models.py
下instance.UserProfile.save()
存在错误
如何获取UserProfile表单以保存并修复此无属性错误?
models.py
from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save
import datetime
from django.conf import settings
class UserProfile(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
blank=True,
null=True,
unique=True,
related_name='userprofile'
)
bio = models.CharField(max_length=200, default='', blank=True)
campus = models.CharField(max_length=100, default='', blank=True)
website = models.URLField(default='', blank=True)
phone = models.IntegerField(default=0, blank=True)
image = models.ImageField(upload_to='profile_image', blank=True)
birthday = models.DateField(blank=True, default=datetime.date.today)
gender = models.CharField(default='', blank=True, max_length=20)
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.UserProfile.save()
forms.py
# Edit User form
class EditProfileForm(UserChangeForm):
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name',
'password',
)
# Edit User Profile form
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = (
'bio',
'campus',
'birthday',
'gender',
'website',
'phone',
'image',
)
views.py
@login_required
@transaction.atomic
def edit_profile(request):
if request.method == 'POST':
user_form = EditProfileForm(request.POST, instance=request.user)
profile_form = UserProfileForm(request.POST, instance=request.user.UserProfile.user)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save(commit=False)
#user.username = user.email
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return redirect('/account/profile')
else:
profile_form = UserProfileForm(instance=request.user.userprofile)
user_form = EditProfileForm(instance=request.user)
args = {
'user_form': user_form, # basic user form
'profile_form': profile_form # user profile form
}
return render(request, 'accounts/edit_profile.html', args)
模板帐户/edit_profile.html
<form method="POST">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<button type="submit">Submit</button>
</form>
urls.py
url(r'^profile/edit/$', views.edit_profile, name='edit_profile')
解决方案:
models.py 中的只有一个接收器,并将else:
instance.UserProfile().save()
更改为else:
instance.userprofile().save()
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
else:
instance.userprofile.save()
在 views.py 中修正拼写错误
将profile_form = UserProfileForm(request.POST, instance=request.user.UserProfile.user)
更改为profile_form = UserProfileForm(request.POST, instance=request.user.userprofile)
答案 0 :(得分:0)
您有两个处理程序在侦听同一信号(post_save
),并且save_user_profile
函数在create_user_profile
找到创建UserProfile
实例的机会之前运行。
我建议将两个处理程序合并为一个,例如:
@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
else:
instance.userprofile.save()