views:

782

answers:

1

I'm trying to follow the guidelines in this answer, but I'm getting stuck with how to edit the template.

The relevant part of my admin.py:

SegmentFormset = forms.models.inlineformset_factory(Division,Segment)
class DivisionForm(forms.ModelForm):
    def __init__(self, **kwargs):
        super(DivisionForm, self).__init__(**kwargs)
        self.segment_formset = SegmentFormset(instance=self.instance, data=self.data,
                prefix=self.prefix)

    def is_valid(self):
        return (super(DivisionForm, self).is_valid() and 
                    self.segment_formset.is_valid())

    def save(self, commit=True):
        assert commit == True 
        res = super(DivisionForm, self).save(commit=commit)
        self.segment_formset.save()
        return res

class DivisionInline(admin.TabularInline):
    model = Division
    form = DivisionForm
    template = 'competitions/admin/tabular.html'

class CompetitionAdmin(VersionAdmin):
    inlines = [DivisionInline,]

The relevant part of my template:

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
         <td>My segment formset should be here</td>
    {% endfor %}

What I can't figure out is how to access the segment formset. I've experimented with all of the variable names and none of them are my DivisionForm. The division formset is the {{fieldset.formset}} variable and that's as far as I've been able to get.

Edit 1:

Actually, the relevant part of the template is a bit longer ;) Putting in more code:

 <tbody>
 {% for inline_admin_form in inline_admin_formset %}
    {% if inline_admin_form.form.non_field_errors %}
    <tr><td colspan="{{ inline_admin_form.field_count }}">{{ inline_admin_form.form.non_field_errors }}</td></tr>
    {% endif %}
    <tr class="{% cycle row1,row2 %} {% if inline_admin_form.original or inline_admin_form.show_url %}has_original{% endif %}">

    <td class="original">
      {% if inline_admin_form.original or inline_admin_form.show_url %}<p>
      {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
      {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
        </p>{% endif %}
      {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
      {{ inline_admin_form.fk_field.field }}
      {% spaceless %}
      {% for fieldset in inline_admin_form %}
        {% for line in fieldset %}
          {% for field in line %}
            {% if field.is_hidden %} {{ field.field }} {% endif %}
          {% endfor %}
        {% endfor %}
      {% endfor %}
      {% endspaceless %}
    </td>

    {% for fieldset in inline_admin_form %}
      {% for line in fieldset.formset %}
        {% for field in line %}
          <td class="{{ field.field.name }}">
          {{ field.field.errors.as_ul }}
          {{ field.field }}
          </td>
        {% endfor %}
      {% endfor %}
      <td>My segment formset should be here</td>
    {% endfor %}

    {% if inline_admin_formset.formset.can_delete %}
      <td class="delete">{% if inline_admin_form.original %}{{ inline_admin_form.deletion_field.field }}{% endif %}</td>
    {% endif %}

    </tr>

 {% endfor %}
 </tbody>
A: 

Formsets are like lists, so you can recurse it like

{% for form in fieldset.formset %}
{{ form.as_p }}
{% endfor %}
jturnbull
Tried that one also. It throws an "object is not iterable" error.
Monika Sulik