4
@admin.register(Personal, site=admin_site)
class PersonalAdmin(admin.ModelAdmin):
form = ChangePersonalForm
add_form = AddPersonalForm
def get_form(self, request, obj=None, **kwargs):
if not obj:
self.form = self.add_form
return super(PersonalAdmin, self).get_form(request, obj, **kwargs)
的第一次嘗試creating
或changing
一個對象,它會正常工作。 但是,之後你create
對象,每一次嘗試change
對象時,你會得到的add_form
代替form
我固定,通過添加一個else塊
@admin.register(Personal, site=admin_site)
class PersonalAdmin(admin.ModelAdmin):
change_form = ChangePersonalForm
add_form = AddPersonalForm
def get_form(self, request, obj=None, **kwargs):
if not obj:
self.form = self.add_form
else:
self.form = self.change_form
return super(PersonalAdmin, self).get_form(request, obj, **kwargs)
好像有某種class
緩存。
任何人都知道爲什麼?
...並且工作順利。 – slackmart