2016-10-19 56 views
0

模型流主題 - >部分 - >文章。Django:在鏈接到UpdateView的表單中從FK更改選項選擇

我正在構建我的FAQ項目的更新視圖以更新已創建的文章。我希望表單根據該文章創建的主題提供選擇的章節。由於我已經通過URL傳遞了文章PK,所以我希望在創建過濾器時使用它來回到主題。當模板嘗試在UpdateAriticleForm中的行self.fields['section'].queryset = Section.objects.filter(topic_id=self.section.topic.id)上渲染窗體時,我收到一個object has no attribute ‘section’錯誤。我需要幫助來找出我的查詢過濾器。

的網址:

url(r'^ironfaq/article/update/(?P<pk>\d+)/$', ArticleUpdateView.as_view()), 

的形式爲:

from django import forms 
from .models import Topic, Section, Article 

class CreateArticleForm(forms.ModelForm): 

    section = forms.ModelChoiceField(queryset=Section.objects.none()) 

    def __init__(self, *args, **kwargs): 
     topic_pk = kwargs.pop('topic_pk') 
     super(CreateArticleForm, self).__init__(*args, **kwargs) 
     self.fields['section'].queryset = Section.objects.filter(topic_id=topic_pk) 

    class Meta: 
     model = Article 
     widgets = { 
      'answer': forms.Textarea(attrs={'data-provide': 'markdown', 'data-iconlibrary': 'fa'}), 
     } 
     fields = ('title','section','answer') 

class UpdateArticleForm(forms.ModelForm): 

    section = forms.ModelChoiceField(queryset=Section.objects.none()) 

def __init__(self, *args, **kwargs): 
    super(UpdateArticleForm, self).__init__(*args, **kwargs) 
    self.fields['section'].queryset = Section.objects.filter(topic_id=self.section.topic.id) 

    class Meta: 
     model = Article 
     widgets = { 
      'answer': forms.Textarea(attrs={'data-provide': 'markdown', 'data-iconlibrary': 'fa'}), 
     } 
     fields = ('title','section','answer') 

的觀點:

class ArticleUpdateView(UpdateView): 
    model = Article 
    form_class = UpdateArticleForm 
    template_name = "faq/form_create.html" 

    def form_valid(self, form): 
     article = form.save(commit=False) 
     article.activity_user = self.request.user.username 
     article.activity_date = datetime.datetime.now() 
     article.save() 
     self.success_url = "/ironfaq/%s/%s/%d" % (article.section.topic.slug,article.section.slug,article.id) 
     return super(ArticleUpdateView,self).form_valid(form) 

種型號:

class Topic(Audit): 
    name = models.CharField(max_length=255) 
    icon = models.CharField(max_length=25,blank=True,null=True) 
    sort = models.SmallIntegerField() 
    slug = models.SlugField() 

    class Meta: 
     verbose_name_plural = "topics" 

    def __str__(self): 
     return self.name 

class Section(Audit): 
    name = models.CharField(max_length=255) 
    sort = models.SmallIntegerField() 
    slug = models.SlugField() 
    topic = models.ForeignKey(Topic,on_delete=models.CASCADE) 

    class Meta: 
     verbose_name_plural = "sections" 

    def __str__(self): 
     return self.name 

class Article(Audit): 
    title = models.CharField(max_length=255) 
    sort = models.SmallIntegerField() 
    slug = models.SlugField() 
    section = models.ForeignKey(Section,on_delete=models.CASCADE) 
    answer = models.TextField() 
    vote_up = models.IntegerField(default=0) 
    vote_down = models.IntegerField(default=0) 
    view_count = models.IntegerField(default=0) 

    class Meta: 
     verbose_name_plural = "articles" 

    def __str__(self): 
     return self.title 

回答

0

的答案,這個問題沒有經過'pk'作爲參數的形式和get_form_kwargs添加到視圖,使形式看到URL通過'pk'

形式:

class UpdateArticleForm(forms.ModelForm): 

    section = forms.ModelChoiceField(queryset=Article.objects.none()) 

    def __init__(self, pk, *args, **kwargs): 
     super(UpdateArticleForm, self).__init__(*args, **kwargs) 
     self.fields['section'].queryset = Section.objects.filter(topic_id__exact=Article.objects.filter(id=pk).first().section.topic.id) 

查看:

class ArticleUpdateView(UpdateView): 
    model = Article 
    form_class = UpdateArticleForm 
    template_name = "faq/form_create.html" 

    def get_form_kwargs(self): 
     kwargs = super(ArticleUpdateView,self).get_form_kwargs() 
     kwargs.update(self.kwargs) 
     return kwargs 

    def form_valid(self, form): 
     article = form.save(commit=False) 
     article.activity_user = self.request.user.username 
     article.activity_date = datetime.datetime.now() 
     article.save() 
     self.success_url = "/ironfaq/%s/%s/%d" % (article.section.topic.slug,article.section.slug,article.id) 
     return super(ArticleUpdateView,self).form_valid(form)