How to manually render a Django template for an inlineformset_factory with can_delete = True / False

697 views
Skip to first unread message

chefsmart

unread,
Apr 15, 2010, 1:45:56 AM4/15/10
to Django users
Hi,

I have an inlineformset with a custom Modelform. So it looks something
like this:

MyInlineFormSet = inlineformset_factory(MyMainModel, MyInlineModel,
form=MyCustomInlineModelForm)

I am rendering this inlineformset manually in a template so that I
have more control over widgets and javascript. So I go in a loop like
{% for form in myformset.forms %} and then manually render each field
as described on this page http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

The formset has can_delete = True or can_delete = False depending on
whether the user is creating new objects or editing existing ones.

Question is, how do I manually render the can_delete checkbox?

Regards.

Tom Evans

unread,
Apr 15, 2010, 6:16:27 AM4/15/10
to django...@googlegroups.com

{% if not form.empty_permitted %}{{ form.DELETE }}{% endif %}


Cheers

Tom

chefsmart

unread,
Apr 15, 2010, 9:00:17 AM4/15/10
to Django users
Thanks for that tip.

I can guess what empty_permitted is, but could you explain how
empty_permitted is related to can_delete? I couldn't find it in the
docs, and such relationship is not apparent from the code.

Regards.

On Apr 15, 3:16 pm, Tom Evans <tevans...@googlemail.com> wrote:


> On Thu, Apr 15, 2010 at 6:45 AM, chefsmart <moran.cors...@gmail.com> wrote:
> > Hi,
>
> > I have an inlineformset with a custom Modelform. So it looks something
> > like this:
>
> > MyInlineFormSet = inlineformset_factory(MyMainModel, MyInlineModel,
> > form=MyCustomInlineModelForm)
>
> > I am rendering this inlineformset manually in a template so that I
> > have more control over widgets and javascript. So I go in a loop like
> > {% for form in myformset.forms %} and then manually render each field

> > as described on this pagehttp://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-fo...

Tom Evans

unread,
Apr 15, 2010, 10:03:03 AM4/15/10
to django...@googlegroups.com
On Thu, Apr 15, 2010 at 2:00 PM, chefsmart <moran....@gmail.com> wrote:
> Thanks for that tip.
>
> I can guess what empty_permitted is, but could you explain how
> empty_permitted is related to can_delete? I couldn't find it in the
> docs, and such relationship is not apparent from the code.
>
> Regards.
>

empty_permitted is an attribute of BaseForm that is set when one does
not mind if the form is submitted without any data, which is the kind
of form that Formset creates for additional (ie new) form instances.

Like many things in django, it isn't documented, so could be
considered to be part of the internal API and changed (although I
doubt it would). I don't see another way of doing the same thing
though. I suppose you could look at the Form and see if it has any
initial data bound to it, but for me this way seems cleaner.

Cheers

Tom

chefsmart

unread,
Apr 15, 2010, 1:01:27 PM4/15/10
to Django users
Makes sense. Thanks.

On Apr 15, 7:03 pm, Tom Evans <tevans...@googlemail.com> wrote:

Reply all
Reply to author
Forward
0 new messages