Alan Tingey Alan Tingey - 1 year ago 303
Python Question

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 =

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download