In Template can I access the object from which a form in a ModelFormset was generated?

I am trying to create a view for doing a stock-take. This will update quantity and a few other things for every Stock item in a queryset. So, a ModelFormset?

However, one of the things that the person doing the stocktake need to do, is check that the description of the item in the database matches the physical items. In order to do this, the view has to display a property object.description.human_description (derived from several fields with various conditionals and formattings) for the object to which the form in the formset relates.

I can't see how to do this in the documentation

{% for form in formset %}
    {{ what.human_description }}   <!-- what is what? -->
    {{form}}
{% endfor %}

Found the answer, sharing for anybody else.

Even as an unbound formset, each form has {{form.instance}} attached.

Any extra forms have "blank" instances. If you are following to a related object, this may cause a crash with RelatedObjectDoesNotExist. So,

{% if form.instance.id %} <!-- its an object that is already in the DB -->
    {{ form.instance.description.human_description }}
{% else %}
    whatever ... this is an extra form with no related description in instance
{% endif %}
Back to Top