简体   繁体   中英

Django - get model object attributes from ModelForm

I have a ModelFormSet:

TransactionFormSet = modelformset_factory(Transaction, exclude=("",))

With this model:

class Transaction(models.Model):
    account = models.ForeignKey(Account)
    date = models.DateField()
    payee = models.CharField(max_length = 100)
    categories = models.ManyToManyField(Category)
    comment = models.CharField(max_length = 1000)
    outflow = models.DecimalField(max_digits=10, decimal_places=3)
    inflow = models.DecimalField(max_digits=10, decimal_places=3)
    cleared = models.BooleanField()

And this is the template:

{% for transaction in transactions %}
<ul>
    {% for field in transaction %}
        {% ifnotequal field.label 'Id' %}
        {% ifnotequal field.value None %}
            {% ifequal field.label 'Categories' %}
                // what do i do here?
            {% endifequal %}
            <li>{{ field.label}}: {{ field.value }}</li>
        {% endifnotequal %}
        {% endifnotequal %}
    {% endfor %}
</ul>
{% endfor %}

The view:

def transactions_on_account_view(request, account_id):
    if request.method == "GET":
        transactions = TransactionFormSet(queryset=Transaction.objects.for_account(account_id))
        context = {"transactions":transactions}
        return render(request, "transactions/transactions_for_account.html", context)

I want to list all the Transaction information on a page. How can I list the "account" property of Transaction and the "categories"? Currently the template shows only their id, I want to get a nice representation for the user (preferrably from their str () method).

The only way I can see that would work is to iterate over the FormSet, get the Ids of the Account and Category objects, get the objects by their Id and store the information I want in a list and then pull it from there in the template, but that seems rather horrible to me.

Is there a nicer way to do this?

Thanks to the comments, I figured it out that what I was doing was pretty dumb and pointless.

This works:

1) Get all Transaction objects

transactions = Transaction.objects.for_account(account_id)

2) Pass to template

context = {"transactions":transactions,}
    return render(request, "transactions/transactions_for_account.html", context)

3) Access attributes, done

  {% for transaction in transactions %}
  <tr>
    <td class="tg-6k2t">{{ transaction.account }}</td>
    <td class="tg-6k2t">{{ transaction.categories }}</td>
    <td class="tg-6k2t">{{ transaction.date }}</td>
    <td class="tg-6k2t">{{ transaction.payee }}</td>
    <td class="tg-6k2t">{{ transaction.comment }}</td>
    <td class="tg-6k2t">{{ transaction.outflow }}</td>
    <td class="tg-6k2t">{{ transaction.inflow }}</td>
    <td class="tg-6k2t">{{ transaction.cleared }}</td>
  </tr>
  {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM