访问Django中的外键字段

huangapple go评论91阅读模式
英文:

How to access Foreign Fields in Django

问题

  1. class Currency (models.Model):
  2. currCode = models.CharField('货币',max_length=3)
  3. currName = models.CharField('名称',max_length=100)
  4. class Spend (models.Model):
  5. spendAmount = models.DecimalField('金额', max_digits=12,decimal_places=2)
  6. spendCurrency = models.ForeignKey(Currency)
英文:

I'm a beginner with Django. I have a set of tables with relationships defined in my models, and I am looking to retrieve linked values in the front end. I'm unable to find out how to do this, and would appreciate help.

I can currently see something that looks like this, where 1 is the key of the currency EUR in the Currency table:

  1. Spend Amount | Spend Currency
  2. 10 | 1

I instead want it to look like this:

  1. Spend Amount | Spend Currency
  2. 10 | EUR

Models

  1. class Currency (models.Model):
  2. currCode = models.CharField('Currency',max_length=3)
  3. currName = models.CharField('Name',max_length=100)
  4. class Spend (models.Model):
  5. spendAmount = models.DecimalField('Amount', max_digits=12,decimal_places=2)
  6. spendCurrency = models.ForeignKey(Currency)

Views

  1. from .models import Spend
  2. def all_spends(request):
  3. spend_list = Spend.objects.all()
  4. return render(request,
  5. 'spends/spend_list.html',
  6. {'spend_list': spend_list})

HTML Template

  1. {% for spend in spend_list %}
  2. <tr>
  3. <th>{{ spend.spendAmount }}</th>
  4. <th>{{ spend.spendCurrency }}</th>
  5. </tr>
  6. {% endfor %}

答案1

得分: 1

直接从您的模板中,您可以使用外键字段直接访问您的货币模型,以显示所需的字段,在您的情况下是 Currency.currName 以显示:

在HTML模板中:

  1. <table>
  2. <thead>
  3. <tr>
  4. <th>Spend Amount</th>
  5. <th>Spend Currency</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. {% for spend in spend_list %}
  10. <tr>
  11. <td>{{ spend.spendAmount }}</td>
  12. <td>{{ spend.spendcurrency.currName }}</td>
  13. </tr>
  14. {% endfor %}
  15. </tbody>
  16. </table>

请注意,还存在一种Django的“related manager”或“Backward relationship”方法来从Currency访问Spend。例如:

  1. {{ my_currency.spend_set.spendAmount }}
英文:

Directly from your template, you can access directly your Currency model using the foreign key field to display the desired field, in your case Currency.currName to display:

  1. Spend Amount | Spend Currency
  2. 10 | EUR

In HTML template

  1. &lt;table&gt;
  2. &lt;/thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;Spend Amount&lt;/th&gt;
  5. &lt;th&gt;Spend Currency&lt;/th&gt;
  6. &lt;/tr&gt;
  7. &lt;/thead&gt;
  8. &lt;tbody&gt;
  9. {% for spend in spend_list %}
  10. &lt;tr&gt;
  11. &lt;td&gt;{{ spend.spendAmount }}&lt;/td&gt;
  12. &lt;td&gt;{{ spend.spendcurrency.currName }}&lt;/td&gt;
  13. &lt;/tr&gt;
  14. {% endfor %}
  15. &lt;/tbody&gt;
  16. &lt;/table&gt;

Note that it also exists a Django "related manager" or "Backward relationship" to access Spend from Currency. For example:

  1. {{ my_currency.spend_set.spendAmount }}

huangapple
  • 本文由 发表于 2023年5月21日 16:27:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定