管理ページでフォームにアクセスしようとすると、次のエラーが表示されます。
/admin/scoring/score/add/super(type, obj) の TypeError: obj はタイプのインスタンスまたはサブタイプでなければなりません
ラインで
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