'How to display value of inline formset in django admin change_form.html

I have 3 inline models in a Parent modelAdmin. I wish to display the value of a field 'contact' present in 1 of the inline models in the Parent ModelAdmin. In change_form.html of the django admin:

{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}


    {% ifequal inline_admin_formset.formset.prefix 'client_executive' %}
    {{ inline_admin_formset.formset.form.contact }}
    {% endifequal%}

    {% include inline_admin_formset.opts.template %}
    {% endfor %}
{% endblock %}

This does not show the value of the populated 'contact' value in the form in the template. The inline model has attribute extra=2. It displays nothing. What is the mistake? How can I access this value easily? Using django 1.6.5



Solution 1:[1]

yes you can do it. Easy than you think:

i wrote article about it

inline in middle of change form

in short terms:

  1. add read only field "contact" in change form.
  2. get inline, which you need, on render this new field "contact".
  3. return render of this inline, don't forget to set only those fields in inline, which you want
  4. Profit.

you need to add around 6 lines of code in you project

    admin.register(Product)
    class ProductModelAdmin(admin.ModelAdmin):
        inlines = (ImageAdminInline,)
        fields = ('title', 'image_inline', 'price')
        readonly_fields= ('image_inline',)  # we set the method as readonly field

        def image_inline(self, obj=None, *args, **kwargs):
            context = obj.response['context_data']
            inline = context['inline_admin_formset'] = context['inline_admin_formsets'].pop(0)
            return get_template(inline.opts.template).render(context, obj.request)

        def render_change_form(self, request, context, *args, **kwargs):
            instance = context['adminform'].form.instance  # get the model instance from modelform
            instance.request = request
            instance.response = super().render_change_form(request, context, *args, **kwargs)
            return instance.response

Solution 2:[2]

I think you have a typo, in Django source I found this: {% for inline_admin_form in inline_admin_formset %} you have an additional 's' {% for inline_admin_formset in inline_admin_formsets %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maxim Danilov
Solution 2 e-nouri