我的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))
开启'提交'为了更新已经创建的个人资料,我得到IntegrityError
。例如,(1062, "Duplicate entry '2' for key 'user_id'")
。显然Django正在尝试添加用户的副本,而不是更新现有用户。
我做错了什么?
答案 0 :(得分:5)
您还需要在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
装饰器,这样就不会遇到匿名用户等问题。