我正在创建一个应用程序,用户可以在其中创建一个project
,每个项目都有一个和一个set of questions
(即一种表单,我称它为Firstquestions)。
我希望用户能够通过表单来编辑/更新此set of questions
。
但是,我得到了错误:
duplicate key value violates unique constraint
我正在使用PostgreSQL
projects / models.py
class Project(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
developer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
projects / urls.py
urlpatterns = [
#Regarding the projects
path('allprojects', views.allprojects, name='allprojects'),
path('createproject', views.createproject, name='createproject'),
path('<int:project_id>', views.projectdetail, name='projectdetail'),
path('<int:project_id>/editproject', views.editproject, name='editproject'),
path('<int:project_id>/deleteproject', views.deleteproject, name='deleteproject'),
#Regarding the set of questions
path('<int:project_id>/', include('firstquestions.urls')),
]
firstquestions / models.py
class Firstquestion(models.Model):
first_one = models.TextField()
first_two = models.TextField()
first_three = models.TextField()
first_four = models.TextField()
first_five = models.TextField()
first_six = models.TextField()
first_seven = models.TextField()
developer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
project = models.OneToOneField(Project, on_delete=models.CASCADE)
firstquestions / urls.py
urlpatterns = [
path('questionstoanswer', views.questionstoanswer, name='questionstoanswer'),
path('firstquestionsdetail', views.firstquestionsdetail, name='firstquestionsdetail'),
path('firstquestionsedit', views.firstquestionsedit, name='firstquestionsedit'),
]
我的编辑功能
firstquestions / views.py
@login_required
def firstquestionsedit(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
if request.POST['first_one'] and request.POST['first_two'] and request.POST['first_three'] and request.POST['first_four'] and request.POST['first_five'] and request.POST['first_seven']:
question = Firstquestion()
question.first_one = request.POST['first_one']
question.first_two = request.POST['first_two']
question.first_three = request.POST['first_three']
question.first_four = request.POST['first_four']
question.first_five = request.POST['first_five']
# question.first_six = request.POST['first_six']
question.first_seven = request.POST['first_seven']
question.developer = request.user
question.project = project
question.save()
messages.success(request, 'Answers for User Centered Design questions have been edited')
return redirect('/projects/allprojects')
else:
return render(request, 'firstquestions/firstquestionsedit.html', {'error':'All fields are required.'})
return render(request, 'firstquestions/firstquestionsedit.html', {'project':project})
答案 0 :(得分:2)
请尝试这个..
@login_required
def firstquestionsedit(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
if request.POST['first_one'] and request.POST['first_two'] and request.POST['first_three'] and request.POST['first_four'] and request.POST['first_five'] and request.POST['first_seven']:
question = Firstquestion.objects.filter(project=project).first()
question.first_one = request.POST['first_one']
question.first_two = request.POST['first_two']
question.first_three = request.POST['first_three']
question.first_four = request.POST['first_four']
question.first_five = request.POST['first_five']
# question.first_six = request.POST['first_six']
question.first_seven = request.POST['first_seven']
question.save()
messages.success(request, 'Answers for User Centered Design questions have been edited')
return redirect('/projects/allprojects')
else:
return render(request, 'firstquestions/firstquestionsedit.html', {'error':'All fields are required.'})
return render(request, 'firstquestions/firstquestionsedit.html', {'project':project})