2017-05-28 64 views
0

在我的django項目中,我創建了三個模型類。類'Subtopic'與類'Chapter'具有ForeignKey關係,類'SAQ'與類'Chapter'和類'Subtopic'具有ForeignKey關係。根據ForeignKey關係在django中過濾表單中的ModelChoiceField選項

#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"] 

我試圖關於「模型SAQ」,使得與特定章節實例相關聯的每個「SAQ形式」,對於模型的選擇字段「二級主題」將僅包含那些的該子主題創建使用Django的ModelForm形式特定的章節實例。

#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'窗體中的選擇字段顯示所有章節實例的所有子主題。任何建議都會非常有幫助。

回答

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) 
相關問題