Django管理自定义窗体不保存
我有在我指定的模板和自定义窗体使用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)
形式的自定义Django管理如下:
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'
我见过引用到管理的save_object方法,但因为我其实希望能挽救我的表单对象,我不认为这是我正在寻找的。任何想法为什么我可能会得到这个错误?
谢谢!
回答:
我想你忘记了return语句
def save(self, *args, **kwargs): print("HI")
return super(Edit_Story_Form, self).save(*args, **kwargs)
以上是 Django管理自定义窗体不保存 的全部内容, 来源链接: utcz.com/qa/263158.html