为了避免代码重复,我想将参数从内联传递到表单,但是我遇到了错误。
我的Inline
:
class SportUserCriteriasInlineAdmin(GenericTabularInline):
model = CTSportOrPlaceTypesToCriteriasGroups
form = CriteriaChoicesFieldForm('user')
我的Admin
:
@admin.register(SportTypesGroups)
class SportTypeGroupsAdmin(admin.ModelAdmin):
inlines = [SportUserCriteriasInlineAdmin]
我的Form
:
class CriteriaChoicesFieldForm(forms.ModelForm):
def __init__(self, user_or_team, *args, **kwargs):
super(CriteriaChoicesFieldForm, self).__init__(*args, **kwargs)
if user_or_team == 'user':
# DO SOMETHING
class Meta:
model = CTSportOrPlaceTypesToCriteriasGroups
fields = '__all__'
行inlines = [SportUserCriteriasInlineAdmin]
生成错误:
File "/home/sweetuser/www/sportproject/lib/python3.5/site-packages/django/contrib/admin/checks.py", line 255, in _check_form
if hasattr(obj, 'form') and not issubclass(obj.form, BaseModelForm):
TypeError: issubclass() arg 1 must be a class
因此,实际传递参数(我通过print()
检查),但由于某种原因,admin
部分存在问题。
答案 0 :(得分:0)
我在这里猜测你的其他一些代码,但是这个怎么样?
admin.py:
class SportUserCriteriasInlineAdmin(GenericTabularInline):
model = CTSportOrPlaceTypesToCriteriasGroups
form = UserCriteriaChoicesFieldForm
class SportTeamCriteriasInlineAdmin(GenericTabularInline):
model = CTSportOrPlaceTypesToCriteriasGroups
form = TeamCriteriaChoicesFieldForm
@admin.register(SportTypesGroups)
class SportTypeGroupsAdmin(admin.ModelAdmin):
inlines = [SportUserCriteriasInlineAdmin]
形式:
class CriteriaChoicesFieldForm(forms.ModelForm):
class Meta:
model = CTSportOrPlaceTypesToCriteriasGroups
fields = '__all__'
class UserCriteriaChoicesFieldForm(CriteriaChoicesFieldForm):
def __init__(self, user_or_team, *args, **kwargs):
super(UserCriteriaChoicesFieldForm, self).__init__(*args, **kwargs)
# DO SOMETHING