Custom form/formset for edit inline

267 views
Skip to first unread message

Emil

unread,
Oct 12, 2008, 3:27:28 PM10/12/08
to Django users
Hi,

I'm trying to figure out how to use a custom form for when editing
objects inline in the admin. The case is something like this:
I have Images that have foreign keys to image galleries. When editing
a gallery, the images associated with it are edited inline. The image
model has quite a few options, so the listing on each gallery page
gets pretty long. Images also have a field for sorting weight. I would
like to implement simons snippet for sorting inline objects with drag
and drop (from http://www.djangosnippets.org/snippets/1053/), which is
pretty unusable with a very long page. I would therefore like the
image form to collapse, showing only the ordering field, title and
hopefully a thumbnail of the image.

I know I could probably do this via a custom template, but that felt a
bit messy for some reason. So, I decided to take a shot at learning a
bit about customizing the admin form stuff via formset factories.

I tried reading the docs, googled a bit but couldn't get it working.
So far I've done the following:

1. Create a normal ModelAdmin-subclass for both Image and ImageGallery
2. Created a ModelForm-subclass for the galleries to get the extra JS
stuff in to get the draggable stuff working
3. Created an ImageInline (subclass of admin.StackedInline)
4. Pointed to the ImageInline from the ModelAdmin options for
ImageGallery
5. In the ImageInline-class, I specified the formset-option, pointing
to...
6. ...the ImageInlineFormset, which a formset created with
inlineformset_factory, that in turn specifies a form in the function
call, namely...
7. ...ImageInlineForm, which is a subclass of ModelForm. There, I've
specified exactly how I want my fieldsets laid out.

My admin.py is up at http://dpaste.com/83994/

Nothing seems to take in changing the inline form for the image
though. I get no errors, but the form stays the same... I'm not 100%
sure about everything I'm doing every step of the way here, so... Have
I got all this backwards? I just assumed that since I can specify a
formset-option on the inline, I should be able to use a formset
created through inlineformset_factory, and, in creating that, pointing
to a custom ModelForm...

//emil

gearheart

unread,
Oct 13, 2008, 6:47:58 AM10/13/08
to Django users
hi,
fieldsets is not a form's attribute
you should put them in ImageInline

V.

On Oct 12, 10:27 pm, Emil <bjorklund.e...@gmail.com> wrote:
> Hi,
>
> I'm trying to figure out how to use a custom form for when editing
> objects inline in the admin. The case is something like this:
> I have Images that have foreign keys to image galleries. When editing
> a gallery, the images associated with it are edited inline. The image
> model has quite a few options, so the listing on each gallery page
> gets pretty long. Images also have a field for sorting weight. I would
> like to implement simons snippet for sorting inline objects with drag
> and drop (fromhttp://www.djangosnippets.org/snippets/1053/), which is
> pretty unusable with a very long page. I would therefore like the
> image form to collapse, showing only the ordering field, title and
> hopefully a thumbnail of the image.
>
> I know I could probably do this via a custom template, but that felt a
> bit messy for some reason. So, I decided to take a shot at learning a
> bit about customizing the admin form stuff via formset factories.
>
> I tried reading the docs, googled a bit but couldn't get it working.
> So far I've done the following:
>
> 1. Create a normal ModelAdmin-subclass for both Image and ImageGallery
> 2. Created a ModelForm-subclass for the galleries to get the extra JS
> stuff in to get the draggable stuff working
> 3. Created an ImageInline (subclass of admin.StackedInline)
> 4. Pointed to the ImageInline from the ModelAdmin options for
> ImageGallery
> 5. In the ImageInline-class, I specified the formset-option, pointing
> to...
> 6. ...the ImageInlineFormset, which a formset created with
> inlineformset_factory, that in turn specifies a form in the function
> call, namely...
> 7. ...ImageInlineForm, which is a subclass of ModelForm. There, I've
> specified exactly how I want my fieldsets laid out.
>
> My admin.py is up athttp://dpaste.com/83994/

Emil

unread,
Oct 13, 2008, 5:27:04 PM10/13/08
to Django users
Hi,

thanks for replying, but I tried that at first, expecting to be able
to do that on the inline, but I couldn't get it working. It was a
little while back, so I'm not sure I remember correctly, but I think I
got some sort of AlreadyRegistered error (might be wrong about that
though).

Also, looking at the docs there is not a "fieldsets"-option for the
Inline admin classes, only for ModelAdmin subclasses, but you seem to
be correct in that there isn't a fieldsets-option for ModelForm. I
guess I assumed it (admin.InlineStacked) somehow inherited from
ModelForm.

Anyhow, I'm still confused about this: In my updated admin.py, I still
reference the formset in my admin.StackedInline-class options, and in
the inlineformset_factory function call specify the fields (trying to
simplify a little - if I can't get a custom fieldset, I thought I'd
just try limiting the fields), but still no change, no errors, just
the same form.

Anyone have any more help/tips? I'm trying to get a grip on this
stuff, read the docs a bunch of times, but it's still a bit confusing
and not doing what I hoped it would... I think I've been staring
myself blind on it now. :-(

//emil

Ben Gerdemann

unread,
Nov 17, 2008, 10:33:03 AM11/17/08
to Django users
I'm having exactly the same problem which I posted about here
http://groups.google.com/group/django-users/browse_thread/thread/bb4c792f13b2eceb#
Have you figured out how to do this? Yours is the third message I've
read by someone trying to customize an inline form without any
solution. I'm beginning to think this probably isn't possible in the
admin interface... :(

Peter Shafer

unread,
Dec 18, 2008, 1:56:44 PM12/18/08
to Django users
I'm also having the same problem as Emil. I have pictures that belong
to galleries so I use inline forms to manage them. I also use the
drag-drop script to order these images. Since images have so many
attributes though, the size of the inline form becomes pretty
unmanageable.

Here is my admin.py
http://dpaste.com/100468/

I don't get any errors, but my inline forms remain unchanged.

Peter

On Nov 17, 10:33 am, Ben Gerdemann <gerd...@gmail.com> wrote:
> I'm having exactly the same problem which I posted about herehttp://groups.google.com/group/django-users/browse_thread/thread/bb4c...

Peter Shafer

unread,
Dec 18, 2008, 5:23:04 PM12/18/08
to Django users
It seems to be working now once I made the following changes to
admin.py http://dpaste.com/100570/

On Dec 18, 1:56 pm, Peter Shafer <peter.sha...@gmail.com> wrote:
> I'm also having the same problem as Emil.  I have pictures that belong
> to galleries so I use inline forms to manage them.  I also use the
> drag-drop script to order these images.  Since images have so many
> attributes though, the size of the inline form becomes pretty
> unmanageable.
>
> Here is my admin.pyhttp://dpaste.com/100468/
Reply all
Reply to author
Forward
0 new messages