mardi 24 février 2015

Django Admin Custom Form Not Saving

I've got a custom Django admin in which I've specified a template and a custom form to use



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)


The form is below:



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)


If I take out the custom save method in the form, it works fine, and the fields of the form save. However, I'm going to need to override save because this form has other forms within it, which I'll be validating upon save.


Adding in my save method makes the page throw up an error:



AttributeError at /admin/Editorial/story/1/
'NoneType' object has no attribute 'save'


I've seen references to the save_object method of the admin, but since I'm actually hoping to save my form object, I don't think this is quite what I'm looking for. Any ideas for why I might be getting this error?


Thanks!


Aucun commentaire:

Enregistrer un commentaire