更新后保存配置文件模型:IntegrityError(重复 user_id 键)
Saving Profile model after update: IntegrityError (duplicate user_id key)
我的 Django 站点有两种用户配置文件,一种用于普通用户 (MyUserProfile
),另一种用于扩展它,用于员工 (EmployeeUserProfile
)。 models.py
:
class MyUserProfile(models.Model):
user = models.OneToOneField('auth.user', related_name='userprofile')
www = models.URLField(null=True, blank=True, verbose_name='website')
affiliation = models.CharField(max_length=200,null=True,blank=True)
...
class EmployeeUserProfile(MyUserProfile):
start_date = models.DateField()
current = models.BooleanField(default=True)
...
我在为员工实施个人资料更新表单时遇到问题。我这样创建表单 (forms.py
):
from django.forms import ModelForm
from .models import EmployeeUserProfile
class EmployeeUserProfileForm(ModelForm):
class Meta:
model = EmployeeUserProfile
exclude = ['user', 'current']
但是当我更新个人资料时:
from django.template import RequestContext
from .forms import EmployeeUserProfileForm
def update_profile(request):
if request.method == 'POST':
form = EmployeeUserProfileForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
else:
user = request.user
profile = user.userprofile.employeeuserprofile
form = EmployeeUserProfileForm(instance=profile)
c = {'form': form}
return render_to_response('pages/profile/update.html', c,
context_instance=RequestContext(request))
在 'submit' 更新已创建的配置文件时,我得到了一个 IntegrityError
。例如,(1062, "Duplicate entry '2' for key 'user_id'")
。显然 Django 试图添加用户的副本而不是更新现有的。
我做错了什么?
您也需要在 POST
条件中传递实例参数
form = EmployeeUserProfileForm(request.POST)
应该是
form = EmployeeUserProfileForm(request.POST, instance=profile)
By not sending the instance
argument, form tries to create
instead of update
。请注意,这意味着您必须将 else
块移动到 if
上方
像这样:
@login_required
def update_profile(request):
user = request.user
profile = user.userprofile.employeeuserprofile
form = EmployeeUserProfileForm(instance=profile)
if request.method == 'POST':
form = EmployeeUserProfileForm(request.POST, instance=profile)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
c = {'form': form}
return render_to_response('pages/profile/update.html', c,
context_instance=RequestContext(request))
您可能还想使用 login_required
装饰器,这样您就不会 运行 陷入匿名用户等问题。
我的 Django 站点有两种用户配置文件,一种用于普通用户 (MyUserProfile
),另一种用于扩展它,用于员工 (EmployeeUserProfile
)。 models.py
:
class MyUserProfile(models.Model):
user = models.OneToOneField('auth.user', related_name='userprofile')
www = models.URLField(null=True, blank=True, verbose_name='website')
affiliation = models.CharField(max_length=200,null=True,blank=True)
...
class EmployeeUserProfile(MyUserProfile):
start_date = models.DateField()
current = models.BooleanField(default=True)
...
我在为员工实施个人资料更新表单时遇到问题。我这样创建表单 (forms.py
):
from django.forms import ModelForm
from .models import EmployeeUserProfile
class EmployeeUserProfileForm(ModelForm):
class Meta:
model = EmployeeUserProfile
exclude = ['user', 'current']
但是当我更新个人资料时:
from django.template import RequestContext
from .forms import EmployeeUserProfileForm
def update_profile(request):
if request.method == 'POST':
form = EmployeeUserProfileForm(request.POST)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
else:
user = request.user
profile = user.userprofile.employeeuserprofile
form = EmployeeUserProfileForm(instance=profile)
c = {'form': form}
return render_to_response('pages/profile/update.html', c,
context_instance=RequestContext(request))
在 'submit' 更新已创建的配置文件时,我得到了一个 IntegrityError
。例如,(1062, "Duplicate entry '2' for key 'user_id'")
。显然 Django 试图添加用户的副本而不是更新现有的。
我做错了什么?
您也需要在 POST
条件中传递实例参数
form = EmployeeUserProfileForm(request.POST)
应该是
form = EmployeeUserProfileForm(request.POST, instance=profile)
By not sending the instance
argument, form tries to create
instead of update
。请注意,这意味着您必须将 else
块移动到 if
像这样:
@login_required
def update_profile(request):
user = request.user
profile = user.userprofile.employeeuserprofile
form = EmployeeUserProfileForm(instance=profile)
if request.method == 'POST':
form = EmployeeUserProfileForm(request.POST, instance=profile)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
c = {'form': form}
return render_to_response('pages/profile/update.html', c,
context_instance=RequestContext(request))
您可能还想使用 login_required
装饰器,这样您就不会 运行 陷入匿名用户等问题。