我有一个自定义Django管理员,我在其中指定了一个模板和一个自定义表单来使用
class StoryAdmin(BaseAdmin):
form = Edit_Story_Form
change_form_template = "CMS/Editorial/AdminStory/AdminStory.html"
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
thing = Story.objects.get(pk=object_id)
extra_context['workflowstate'] = thing.workflowstate.title()
extra_context['last_updated'] = thing.updateddate
return super(StoryAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context)
admin.site.register(Story, StoryAdmin)
表格如下:
class Edit_Story_Form(ModelForm):
headline = forms.CharField()
subheadline = forms.CharField()
add_block = forms.CharField(widget=forms.HiddenInput(), label='', required=False)
class Meta:
model = Story
fields = []
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
request = self.request
super(Edit_Story_Form, self).__init__(*args, **kwargs)
zones = Zone.objects.filter(story=self.instance)
for zone in zones:
zone.form = Edit_Zone_Form(
request.POST or None,
request.FILES or None,
instance = zone,
prefix = "zone_"+str(zone.id),
)
blocks = Block.objects.filter(zone=zone).filter(has_parent=False)
for block in blocks:
block_forms = {
"Text": Edit_Text_Block_Form,
"Image": Edit_Image_Block_Form,
"Video": Edit_Video_Block_Form,
"Embed": Edit_Embed_Block_Form,
"Code": Edit_Code_Block_Form,
"Live Blog":Edit_Live_Blog_Block_Form,
"Tweet": Edit_Tweet_Block_Form,
"Quiz": Edit_Quiz_Block_Form,
"Slideshow":Edit_Slideshow_Block_Form,
"Curated": Edit_Curated_Block_Form
}
block.form = block_forms[block.block_type](
request.POST or None,
request.FILES or None,
instance = block.get_instance(),
prefix = "block_"+str(block.id),
)
def save(self, *args, **kwargs):
print("HI")
super(Edit_Story_Form, self).save(*args, **kwargs)
如果我在表单中取出自定义保存方法,它可以正常工作,并保存表单的字段。但是,我将需要覆盖保存,因为此表单中包含其他表单,我将在保存时进行验证。
添加我的保存方法会使页面出错:
AttributeError at /admin/Editorial/story/1/
'NoneType' object has no attribute 'save'
我见过对admin的save_object方法的引用,但由于我实际上希望保存我的表单对象,所以我认为这不是我想要的。为什么我可能会收到此错误的任何想法?
谢谢!
答案 0 :(得分:2)
我想你忘记了回复陈述
def save(self, *args, **kwargs):
print("HI")
return super(Edit_Story_Form, self).save(*args, **kwargs)