Django forms access (file) field from relation

I have a view that locks like this:

  def table_form_combined(request: HttpRequest) -> HttpResponse:
    if request.method == "POST":

        formset = TransActionFormSet(data=request.POST)
        print(formset.errors)
        if formset.is_valid():
            formset.save()

    return render(request, "evidence/table_form_combined.html", {'formset': TransActionFormSet})

My Form looks like this:

class TransactionForm(ModelForm):
    class Meta:
        model = Transaction
        fields = ("source_id", "transaction_date", "data", "evidence", "meta_data")
        widgets = {
            'data': Textarea(attrs={'rows': 1}),  # set the row amount of
            # the data Text fields to 1 --> data : Name of Textfield in Model
        }

My model (just the relevant stuff):

class TransactionEvidence(Base):
    file = models.FileField(upload_to='evidence/')
class Transaction(Base):

    evidence = models.ForeignKey(
        TransactionEvidence,
        on_delete=models.CASCADE,
        help_text="A evidence file (i.e. pdf invoice) that can be assigned to a transaction.",
    )

    source_id = models.CharField(
        "Source ID", max_length=150
    )

    transaction_date = models.DateTimeField(default=timezone.now, help_text="The transaction date")

And finally, my template locks like this:

{% extends "website/base.html" %}
{% block content %}

    <table class="table">
        <thead>
            <tr>
                <th scope="col"> # </th>
                <th scope="col"> Name </th>
                <th scope="col"> Transaction Date </th>
                <th scope="col"> Data</th>
                <th scope="col"> Evidence</th>
                <th scope="col"> file</th>
                <th scope="col"> Amount</th>
            </tr>
        </thead>
        <tbody>
        {{ formset.management_form }}
        {% comment %}
            https://docs.djangoproject.com/en/3.2/topics/forms/formsets/#understanding-the-managementform,
            without that, the form will be invalid
        {% endcomment %}
        {% for form in formset  %}
        <form method="post" enctype="multipart/form-data">

            {% csrf_token %}
            {% if form.source_id.value %}

            <tr>
                <td>
                    {{form.id.value}}
                    {{form.id.as_hidden}}
                </td>
                <td>
                    {{form.source_id.value}}
                    {{form.source_id.as_hidden}}
                </td>
                <td>
                    {{form.transaction_date.value }}
                    {{form.transaction_date.as_hidden }}
                </td>
                <td>
                    {{form.data.value}}
                    {{form.data.as_hidden}}
                </td>
                <td>
                    {{form.evidence}}
                    {{form.as_hidden}}
                </td>
                <td>
                    {{form.instance.meta_data.amount}}
                    {{form.meta_data.as_hidden}}
                </td>
                <td>
                    <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                </td>
            </tr>
            {% endif %}
            </form>

        {% endfor %}
        </tbody>

    </table>
    <div class="offset-10 col-2">
    </div>
{% endblock %}

I'm new to django and want to archive a customizable table where I can Update Data. But only the files. Right now it locks like this: enter image description here

And this is pretty close to what I want, but can somebody help me and explain on how to add the file field of the TransactionEvidence Model?

Thanks!

Back to Top