将表单迁移到modelform

时间:2017-02-19 19:18:07

标签: django forms django-forms modelform

我写了以下表格:

class VoteForm(forms.Form):
    choice = forms.ModelChoiceField(queryset=None, widget=forms.RadioSelect)

    def __init__(self, *args, **kwargs):
        question = kwargs.pop('instance', None)
        super().__init__(*args, **kwargs)

        if question:
            self.fields['choice'].queryset = question.choice_set

class VoteView(generic.UpdateView):
    template_name = 'polls/vote.html'
    model = Question
    form_class = VoteForm

    def get_queryset(self):
        return Question.objects.filter(pub_date__lte=timezone.now()).exclude(choice__isnull=True)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Check duplicate vote cookie
        cookie = self.request.COOKIES.get(cookie_name)
        if has_voted(cookie, self.object.id):
            context['voted'] = True
        return context

    def get_success_url(self):
        return reverse('polls:results', args=(self.object.id,))

    def form_valid(self, form):
        redirect = super().form_valid(form)

        # Set duplicate vote cookie.
        cookie = self.request.COOKIES.get(cookie_name)
        half_year = timedelta(weeks=26)
        expires = datetime.utcnow() + half_year
        if cookie and re.match(cookie_pattern, cookie):
            redirect.set_cookie(cookie_name, "{}-{}".format(cookie, self.object.id), expires=expires)
        else:
            redirect.set_cookie(cookie_name, self.object.id, expires=expires)

        return redirect

问题是普通形式不代表对象没有像ModelForm那样的save()方法。但我无法弄清楚如何迁移表单。没有选择或choice_set字段:

class VoteForm(forms.ModelForm):
    class Meta:
        Model = Question
        #throws exception
        fields = ('choice',)
        widgets = {
            'choice': forms.RadioSelect()
                   }

编辑: 以下是模型:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

如何将上面的表格复制为模型?

1 个答案:

答案 0 :(得分:1)

即使您有ModelForm,也可以根据需要定义其他字段。在您的情况下,它将是以前正常形式的选择字段。

然后在Meta中,您将排除问题模型中不需要的所有字段。

在init之后,您将为选择字段提供选择的提供实例集。

class VoteForm(forms.ModelForm):
    choice = forms.ModelChoiceField(queryset=None, widget=forms.RadioSelect)

    class Meta:
        model = Question
        exclude = ['question_text','pub_date']

    def __init__(self, *args, **kwargs):
        super(VoteForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        if instance:
            self.fields['choice'].queryset = instance.choice_set

代码是在线编写的,未经过测试,但我认为它应该可行。