Djnago 表单在编辑记录时出现错误

发布于 2025-01-11 19:10:46 字数 3208 浏览 0 评论 0原文

我在根据 CHatquestion ID 编辑记录时遇到问题,如果选项为空,那么我需要根据相同的 chatquestion id 添加记录,如果选项中存在 chatqustion id 它将起作用, 我正在尝试多种方法来解决这个问题,但仍然找不到解决方案。

Models.py # thease 都是 3 个模型

class Problem(models.Model):
    Language = models.IntegerField(choices=Language_CHOICE, default=1)
    type = models.CharField(max_length=500, null=True, blank=True)

    def __str__(self):
        return self.type

class ChatQuestion(models.Model):  # Eding record based on chatquestion id  
    question = RichTextField(null=True, blank=True)
    problem_id = models.ForeignKey(
        Problem,
        models.CASCADE,
        verbose_name='Problem',
    )

    def __str__(self):
        return self.question
    is_first_question = models.BooleanField(default=False)

class Option(models.Model):
    option_type = models.CharField(max_length=250, null=True, blank=True)
    question_id = models.ForeignKey(
        ChatQuestion,
        models.CASCADE,
        verbose_name='Question',
        null=True,
        blank=True
    )
    problem=models.ForeignKey(
        Problem,
        models.CASCADE,
        verbose_name='Problem',
        null=True,
        blank=True
    )
    next_question_id = models.ForeignKey(ChatQuestion, on_delete=models.CASCADE, null=True, blank=True,
                                         related_name='next_question')

    def __str__(self):
        return self.option_type

forms.py

class EditQuestionForm(forms.ModelForm):
    class Meta:
        model = ChatQuestion
        fields =('question','problem_id')

class EditOptionForm(forms.ModelForm):
    class Meta:
        model = Option
        fields =('option_type',)

views.py

def question_edit(request,id=None):
    if id is not None:
        queryset = get_object_or_404(ChatQuestion,pk=id)
        queryset1=get_object_or_404(Option,question_id=queryset )
    else:
        queryset = None
        queryset1 = None
    if request.method=="POST":
        form = EditQuestionForm(request.POST ,instance=queryset)
        form1=EditOptionForm(request.POST, instance=queryset1)
        if form.is_valid() and form1.is_valid():
            question=form.cleaned_data['question']
            option_type=form1.cleaned_data['option_type']
            if id:
                queryset.question=question
                
                queryset.save()
                queryset1.option_type=option_type
                
                queryset1.save()
                messages.success(request,'Sucessful')
                return redirect('/fleet/list_chatbot')
            else:
                print(form.errors)
                messages.error(request,'Please correct following',form.errors)
    elif id:
        form = EditQuestionForm(instance=queryset)
        form1=EditOptionForm(instance=queryset1)
     
        if not queryset1:
            form1=EditOptionForm()
            

    else:
        form = EditQuestionForm()
        form1=EditOptionForm()
    context={
        'form':form,
        'form1':form1
    }
    return render(request,'chatbot/question_edit.html',context=context)

I am getting Issue while edit a record based on CHatquestion ID, if option is null then i need to add a record based on same chatquestion id, if chatqustion id exist in option it will work,
i am trying to multiple way to solve this issue but still can't find solution.

Models.py # thease are all 3 models

class Problem(models.Model):
    Language = models.IntegerField(choices=Language_CHOICE, default=1)
    type = models.CharField(max_length=500, null=True, blank=True)

    def __str__(self):
        return self.type

class ChatQuestion(models.Model):  # Eding record based on chatquestion id  
    question = RichTextField(null=True, blank=True)
    problem_id = models.ForeignKey(
        Problem,
        models.CASCADE,
        verbose_name='Problem',
    )

    def __str__(self):
        return self.question
    is_first_question = models.BooleanField(default=False)

class Option(models.Model):
    option_type = models.CharField(max_length=250, null=True, blank=True)
    question_id = models.ForeignKey(
        ChatQuestion,
        models.CASCADE,
        verbose_name='Question',
        null=True,
        blank=True
    )
    problem=models.ForeignKey(
        Problem,
        models.CASCADE,
        verbose_name='Problem',
        null=True,
        blank=True
    )
    next_question_id = models.ForeignKey(ChatQuestion, on_delete=models.CASCADE, null=True, blank=True,
                                         related_name='next_question')

    def __str__(self):
        return self.option_type

forms.py

class EditQuestionForm(forms.ModelForm):
    class Meta:
        model = ChatQuestion
        fields =('question','problem_id')

class EditOptionForm(forms.ModelForm):
    class Meta:
        model = Option
        fields =('option_type',)

views.py

def question_edit(request,id=None):
    if id is not None:
        queryset = get_object_or_404(ChatQuestion,pk=id)
        queryset1=get_object_or_404(Option,question_id=queryset )
    else:
        queryset = None
        queryset1 = None
    if request.method=="POST":
        form = EditQuestionForm(request.POST ,instance=queryset)
        form1=EditOptionForm(request.POST, instance=queryset1)
        if form.is_valid() and form1.is_valid():
            question=form.cleaned_data['question']
            option_type=form1.cleaned_data['option_type']
            if id:
                queryset.question=question
                
                queryset.save()
                queryset1.option_type=option_type
                
                queryset1.save()
                messages.success(request,'Sucessful')
                return redirect('/fleet/list_chatbot')
            else:
                print(form.errors)
                messages.error(request,'Please correct following',form.errors)
    elif id:
        form = EditQuestionForm(instance=queryset)
        form1=EditOptionForm(instance=queryset1)
     
        if not queryset1:
            form1=EditOptionForm()
            

    else:
        form = EditQuestionForm()
        form1=EditOptionForm()
    context={
        'form':form,
        'form1':form1
    }
    return render(request,'chatbot/question_edit.html',context=context)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文