我为注册用户创建了一个模型,并在其中添加了字段反馈。我需要允许已登录的用户发布反馈,并在模型中进行更新,但事实并非如此。相反,它是作为具有主键的新条目创建的。
注册模型
class UserProfile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE, default=None, null=True)
role = models.CharField(max_length=50, choices=Roles, default='client')
feedback = models.TextField(max_length=500,blank=True)
verified =models.BooleanField(default = False,blank=True)
def __str__(self):
return self.user.username
反馈表:
class FeedbackForm(forms.ModelForm):
class Meta():
model = UserProfile
fields = ('feedback',)
查看反馈:
@login_required
def feedback(request):
if request.method == "POST":
form = FeedbackForm(request.POST)
else:
form = FeedbackForm()
if form.is_valid():
userprofile=form.save(request)
userprofile.save()
else:
form = FeedbackForm()
return render(request, 'NewApp/feedback.html',{'form':form})
答案 0 :(得分:0)
您需要将当前配置文件作为instance
参数传递。
form = FeedbackForm(request.POST, instance=request.user.userprofile)