根据ForeignKey关系

时间:2017-05-28 17:36:08

标签: python django django-models django-forms django-views

在我的django项目中,我创建了三个模型类。课程' Subtopic'与课程'章'有ForeignKey关系。和班级' SAQ'与课程'章'有ForeignKey关系。和班级' Subtopic'。

#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为模型SAQ'创建表单。这样,对于每个SAQ表格来说,与特定章节实例相关联的模型的选择字段&Subchanic'将仅包含该特定章节实例的那些子主题。

#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 :(得分: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)