Newforms Admin - Overriding the Save Method of an Inline Model

862 views
Skip to first unread message

John Boxall

unread,
Jul 2, 2008, 2:22:39 PM7/2/08
to Django users
Hey everyone -

I'm running into a bit of a problem with NFA.

I would like to override the save method of an inline model.

For example -
I have a Poll which many Options.
In the admin panel I would be able to edit the Options while editing
the Poll so I declare the Poll to have the Options as an inline.

Now, when the Save button is pressed the Poll is saved and the Options
are saved - however I would like to add some custom behavior when each
individual Option is saved.

Lllama's NFA guide talks about custom validation:
http://code.djangoproject.com/wiki/NewformsHOWTO#Q:HowdoIaddcustomvalidation

I've implemented a similiar approach, setting the ModelAdmin to point
to a custom ModelForm and defining a ``clean`` method. This method
always runs on each of the Options whenever the Poll form (with the
Options inline) is saved.

However the ``save`` method I've declared is never run.

Any thoughts?

Cheers,

John

---

class OptionModelForm(forms.ModelForm):

class Meta:
model = Option

def save(self, commit=True):
# Will -NEVER- run
assert False

def clean(self):
# Will -ALWAYS- run
assert False

...

class OptionInline(admin.TabularInline):
model = Option
form = PollModelForm

class PollAdmin(admin.ModelAdmin):
model = Poll
inlines = [OptionInline]

Brian Rosner

unread,
Jul 2, 2008, 2:47:20 PM7/2/08
to django...@googlegroups.com

On Jul 2, 2008, at 12:22 PM, John Boxall wrote:

> class OptionModelForm(forms.ModelForm):
>
> class Meta:
> model = Option
>
> def save(self, commit=True):
> # Will -NEVER- run
> assert False
>
> def clean(self):
> # Will -ALWAYS- run
> assert False
>
> ...
>
> class OptionInline(admin.TabularInline):
> model = Option
> form = PollModelForm
>
> class PollAdmin(admin.ModelAdmin):
> model = Poll
> inlines = [OptionInline]
>
>

Ok, based on the wrongly posted thread on django-developers, you
mentioned it was a form on a ModelAdmin /not/ an inline. Right now
save is not called on a ModelForm from an inline. I am still not sure
if it ever will. It might end up being a documentation fix. Check into
how model formsets work. It might help you uncover why this is.

> >

Brian Rosner
http://oebfare.com

Richard Dahl

unread,
Jul 2, 2008, 2:53:13 PM7/2/08
to django...@googlegroups.com
You may be able to do what you want to by overriding the save() function within the model. If you need any information from the form or related object you could pass them as kwargs to save.
-richard

John Boxall

unread,
Jul 2, 2008, 3:33:43 PM7/2/08
to Django users
Hey Brian - I wasn't sure that the fact that it was inline would have
an effect - I see now that it does - sorry for the incomplete first
post.
I just found it odd that what I was doing worked for ``clean`` but not
``save``.
I'll look into Formsets.

Richard - I'm sure I could do it that way - but I'd rather not to ...

The Poll model is generic and I would be putting application specific
logic inside that generic class.
Then again, maybe it's best just to do that and avoid the headache :)

Cheers,

John


On Jul 2, 11:53 am, "Richard Dahl" <rich...@dahl.us> wrote:
> You may be able to do what you want to by overriding the save() function
> within the model. If you need any information from the form or related
> object you could pass them as kwargs to save.http://www.djangoproject.com/documentation/model-api/
> -richard
Reply all
Reply to author
Forward
0 new messages