0

每当我尝试在管理页面中访问我的表单时,我都会收到以下错误。

TypeError at /admin/scoring/score/add/ super(type, obj): obj must be an instance or subtype of type

在线

AdminForm = super(ScoreForm, self).get_form(request, obj, **kwargs)

我试图在表单初始化期间访问会话,因此我可以在整个会话期间保留菜单小部件中选择的内容。我正在关注这篇文章。我找不到我做错了什么。

class ScoreForm(forms.ModelForm):
def __init__(self, *args, **kwargs):

    super(ScoreForm, self).__init__(*args, **kwargs)
    self.request = kwargs.pop('request', None)

    standard_events = Event.objects.filter(event_score_type='STD')
    event_widget = self.fields['event'].widget

    choices = []
    for element in standard_events:
        choices.append((element.id, element.name))
    event_widget.choices = choices

def clean_score(self):
    print self.request.session
    print(self.cleaned_data['event'])
    selected_event = Event.objects.get(name=self.cleaned_data['event'])
    if(selected_event.max_score > selected_event.min_score):
        if self.cleaned_data['score'] < 0:
            raise forms.ValidationError("Score Cannot Be Negative")
        elif self.cleaned_data['score'] > selected_event.max_score:
            raise forms.ValidationError("Score Cannot Be Greater Than Best Possible Score of " + str(selected_event.max_score))
        elif self.cleaned_data['score'] < selected_event.min_score:
            raise forms.ValidationError("Score Cannot Be Less Than Worst Possible Score of " + str(selected_event.min_score))
        else:
            return self.cleaned_data['score']
    elif selected_event.max_score < selected_event.min_score:
        if self.cleaned_data['score'] < 0:
            raise forms.ValidationError("Score Cannot Be Negative")
        elif self.cleaned_data['score'] < selected_event.max_score:
            raise forms.ValidationError("Score Cannot Be Less Than Best Possible Score of " + str(selected_event.max_score))
        elif self.cleaned_data['score'] > selected_event.min_score:
            raise forms.ValidationError("Score Cannot Be Greater Than Worst Possible Score of " + str(selected_event.min_score))
        else:
            return self.cleaned_data['score']
    class Meta:
        model = Score


class ScoreAdmin(admin.ModelAdmin):
    form = ScoreForm

    def get_form(self, request, obj=None, **kwargs):

        #Line Below Causes Error
        AdminForm = super(ScoreForm, self).get_form(request, obj, **kwargs)

        class ModelFormMetaClass(AdminForm):
            def __new__(cls, *args, **kwargs):
                kwargs['request'] = request
                return AdminForm(*args, **kwargs)

        return ModelFormMetaClass
4

0 回答 0