简体   繁体   中英

Django ValueError: ModelForm has no model class specified

I have the following code which complains about the following error:

ValueError: ModelForm has no model class specified.

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)

Any help would be greatly appreciated. Many thanks, Alan.

The error message is clearly telling you that you have not specified a model class.

For a ModelForm, you have to use Model class:

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

If this isn't a form based on a model, don't inherit from forms.ModelForm, just use an ordinary forms.Form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM