2

In my Django app (research database), when changing a person object in the admin, I'd like all of the sources for that person to be listed as hyperlinks to the file for that source. I'm trying to do this by creating a custom template for a stacked inline. Here is the custom template so far:

<p>Testing</p>

{% for form in inline_admin_formset %}
    {% for fieldset in form %} 
        <h5>Fieldset</h5>
        {% if fieldset.name %} <h2>{{ fieldset.name }}</h2>{% endif %}
        {% for line in fieldset %} 
            <h6>Line</h6>
            {% for field in line %} 
                <h6>Field</h6>
                {{ field.field }}       
            {% endfor %}
        {% endfor %}
    {% endfor %} 
{% endfor %}

A lot of this is just for me to see what's going on. I used the links here and here as sort of a guide. What renders from the {{ field.field }} is what you'd expect from an inline element - a dropdown menu with the source names as choices and some icons for adding/changing.

For clarity.

What I really want, however, is just the source name rendered as a hyperlink. How do I get the source name (the actual name of the attribute is source_name) from what I have in the Django template language (i.e. the "field" object)?

1 Answers1

2

In that context, {{ field.field }} is a BoundField object, and the value method is probably what you would want to use, as is in {{ field.field.value }}.

A more Django-ish approach (and more complicated) might involve creating a custom widget (start by subclassing one of their built-ins) that only displays text, and then hook that into the form being used in the ModelAdmin for your model. I think there's a bit of a rabbit hole there, in terms of needing to subclass the BaseInlineFormset and possibly a few others down that chain... I'm seeing that the BaseFormSet class has a .form attribute referenced in its construct_form method, but things are little murky from there.

Might also be useful to check out this past thread: Override a form in Django admin

YellowShark
  • 2,169
  • 17
  • 17