Django ValueError:ModelForm没有指定模型类

时间:2016-08-28 23:09:58

标签: python django

我有以下代码抱怨以下错误:

ValueError:ModelForm没有指定模型类。

from django import forms
from straightred.models import StraightredTeam
from straightred.models import UserSelection



class SelectTwoTeams1(forms.Form):

    campaignnoquery = UserSelection.objects.filter(user=349).order_by('-campaignno')[:1]
    currentCampaignNo = campaignnoquery[0].campaignno
    cantSelectTeams = UserSelection.objects.filter(campaignno=currentCampaignNo)

    currentTeams = StraightredTeam.objects.filter(currentteam = 1).exclude(teamid__in=cantSelectTeams.values_list('teamselectionid', flat=True))
    team_one = forms.ModelChoiceField(queryset = currentTeams)
    team_two = forms.ModelChoiceField(queryset = currentTeams)



class SelectTwoTeams(forms.ModelForm):
    used_his = forms.ModelMultipleChoiceField(queryset=UserSelection.objects.filter(user__id=1))

    def __init__(self, user, *args, **kwargs):
        super(SelectTwoTeams, self).__init__(*args, **kwargs)
        self.fields['used_his'].queryset = User.objects.filter(pk = user.id)

非常感谢任何帮助。非常感谢,艾伦。

1 个答案:

答案 0 :(得分:1)

错误消息明确告诉您尚未指定模型类。

对于ModelForm,您必须使用Model类:

class ProductForm(forms.ModelForm):
    class Meta:
        model = Product

如果这不是基于模型的表单,不要从forms.ModelForm继承,只需使用普通的forms.Form。