首页 文章

根据ForeignKey关系在django中的表单中过滤ModelChoiceField的选项

提问于
浏览
0

在我的django项目中,我创建了三个模型类 . 类'Subtopic'与类'Chapter'具有ForeignKey关系,类'SAQ'与类'Chapter'和类'Subtopic'具有ForeignKey关系 .

#models.py

from django.db import models
class Chapter(models.Model):
    chapter_serial = models.IntegerField(null=False, help_text="Chapter No.")
    slug = models.SlugField(unique=True, blank=True)
    chapter_name = models.CharField(max_length=120)

    class Meta:
        ordering = ["chapter_serial"]

    def get_saq_list_url(self):
        return reverse("cms:saq_list", kwargs={"chap_slug":self.slug})

class Subtopic(models.Model):
    subtopic_serial = models.IntegerField(null=False)
    title = models.CharField(max_length=240)
    chapter = models.ForeignKey('Chapter', on_delete=models.CASCADE)

    class Meta:
        ordering = ["subtopic_serial"]

class SAQ(models.Model):
    question_serial = models.IntegerField(null=False)
    question = models.TextField()
    answer = models.TextField()
    chapter = models.ForeignKey('Chapter', on_delete=models.CASCADE)
    subtopic = models.ForeignKey('Subtopic', on_delete=models.CASCADE, null=True, blank=True)

    class Meta:
        ordering = ["question_serial"]

我尝试使用django ModelForm为'model SAQ'创建表单,这样对于与特定章节实例相关联的每个'SAQ表单',模型'Subtopic'的选择字段将仅包含该特定章节实例的那些子主题 .

#forms.py
from django import forms
from .models import SAQ

class SAQForm(forms.ModelForm):

    class Meta:
        model = SAQ
        fields = [
            'question_serial',
            'question',
            'answer',
            'important',
            'remarks',
            'subtopic',
        ]

用于创建表单的django视图函数如下所示 .

from django.shortcuts import render, get_object_or_404, redirect
from .models import SAQ, Chapter, Subtopic
from .forms import SAQForm
from django.http import HttpResponseRedirect

def saq_create(request, chap_slug=None):
    chapter_instance = get_object_or_404(Chapter, slug=chap_slug)
    form = SAQForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.chapter = chapter_instance
        instance.save()
        return HttpResponseRedirect(chapter_instance.get_saq_list_url())
    context = {
        "form":form,
        "chapter_instance":chapter_instance,
    }
    return render(request, 'cms/saq_form.html', context)

使用此配置,表单中的选项字段“Subtopic”显示所有章节实例的所有子主题 . 任何建议都会非常有帮助 .

1 回答

  • 0

    我建议覆盖表单init并传递章节实例,以便您可以过滤子主题查询集 .

    示例(未测试,可能包含拼写错误):

    #forms.py
    from django import forms
    from .models import SAQ
    
    class SAQForm(forms.ModelForm):
    
        class Meta:
            model = SAQ
            fields = [
                'question_serial',
                'question',
                'answer',
                'important',
                'remarks',
                'subtopic',
            ]
    
        def __init__(self, chapter, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['subtopic'].queryset = \
                self.fields['subtopic'].queryset.filter(chapter=chapter)
    

    然后你的视图应该将章节实例传递给表单:

    chapter_instance = get_object_or_404(Chapter, slug=chap_slug)
    form = SAQForm(chapter_instance, request.POST or None)
    

相关问题