от
Я создаю приложение, в котором пользователь может создать
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('', views.projectdetail, name='projectdetail'),
    path('/editproject', views.editproject, name='editproject'),
    path('/deleteproject', views.deleteproject, name='deleteproject'),
    #Regarding the set of questions
    path('/', include('firstquestions.urls')),

]
firstquestions/urls.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})

Пожалуйста, войдите или зарегистрируйтесь для публикации ответа на этот вопрос.

Добро пожаловать на сайт ByNets, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...