[英]Django - Foreign Key not working
我正在嘗試使用foreach循環和django在網頁上顯示一些數據。 我似乎不明白如何使用我在數據庫中創建的查找表。
這些是DB中的列:
budget_audit_table:
-BudgetID
-BudgetTypeID <- Foreign Key
-ObjectAuditID
-CustomerID
-DateOfTransaction
-BudgetObject
-Amount
budget_type:
-BudgetTypeID
-BudgetType
您可以在model.py中假設,BudgetTypeID是外鍵。 在budget_type表中,我目前有2行: - 費用:ID 1 - 收入:ID 2
現在我遇到的問題是我已經搜索了幾天,現在試圖更多地了解Django的API,我很難理解我如何從budget_audit_table中顯示每一行,我如何而不是顯示BudgetTypeID (例如1),它顯示BudgetType(例如費用)?
這是我的觀點和模板
view.py
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.template import RequestContext, loader
from models import BudgetAuditTable
from models import BudgetType
@login_required
def index(request):
budgetauditlist = BudgetAuditTable.objects.order_by('-budgetid')
template = loader.get_template('budget/budget.html')
context = RequestContext(request, {
'budgetauditlist': budgetauditlist,
})
return HttpResponse(template.render(context))
模板
{% for budgetauditobject in budgetauditlist %}
<tr>
<td>{{ budgetauditobject.budgetid }}</td>
<td>{{ budgetauditobject.budgettypeid }}</td>
<td>{{ budgetauditobject.objectauditid }}{{ budgetauditobject.customerid }}</td>
<td>{{ budgetauditobject.amount }}</td>
</tr>
{% endfor %}
models.py
class BudgetAuditTable(models.Model):
budgetid = models.IntegerField(db_column='BudgetID', primary_key=True)
budgettypeid = models.ForeignKey('BudgetType', db_column='BudgetTypeID', blank=True, null=True)
objectauditid = models.IntegerField(db_column='ObjectAuditID', blank=True, null=True)
customerid = models.IntegerField(db_column='CustomerID', blank=True, null=True)
dateoftransaction = models.DateField(db_column='DateOfTransaction', blank=True, null=True)
budgetobject = models.CharField(db_column='BudgetObject', max_length=255, blank=True)
amount = models.DecimalField(db_column='Amount', max_digits=10, decimal_places=2, blank=True, null=True)
class Meta:
managed = False
db_table = 'budget_audit_table'
class BudgetType(models.Model):
budgettypeid = models.IntegerField(db_column='BudgetTypeID', primary_key=True)
budgettype = models.CharField(db_column='BudgetType', max_length=25, blank=True)
class Meta:
managed = False
db_table = 'budget_type'
在您的模板中嘗試:
{% for budgetauditobject in budgetauditlist %}
<tr>
<td>{{ budgetauditobject.budgetid }}</td>
<td>{{ budgetauditobject.budgettypeid.budgettype }}</td>
<td>{{ budgetauditobject.objectauditid }}{{ budgetauditobject.customerid }}</td>
<td>{{ budgetauditobject.amount }}</td>
</tr>
{% endfor %}
此外,您應該將模型中的BudgetType
ForeignKey字段簡單地重命名為BudgetType
因為您的ForeignKey關系代表另一個實體。
有關關系查找,請參閱此鏈接 。
budgetauditobject.budgettypeid
指定BudgetType對象,您應該使用BudgetType下的屬性,如下所示:
{{ budgetauditobject.budgettypeid.budgettype }}
{{ budgetauditobject.budgettypeid.budgettypeid }}
所以,你需要像這樣修改模板:
{% for budgetauditobject in budgetauditlist %}
<tr>
<td>{{ budgetauditobject.budgetid }}</td>
<td>{{ budgetauditobject.budgettypeid }}</td>
<td>{{ budgetauditobject.objectauditid }}{{ budgetauditobject.customerid }}</td>
<td>{{ budgetauditobject.amount }}</td>
</tr>
{% endfor %}
{{ budgetauditobject.budgettypeid.budgettype }}
它非常簡單,只需遵循這樣的關系 ,就像這樣:
{% for budgetauditobject in budgetauditlist %}
<tr>
<td>{{ budgetauditobject.budgetid }}</td>
<td>{{ budgetauditobject.budgettypeid.budgettype }}</td>
<td>{{ budgetauditobject.objectauditid }}{{ budgetauditobject.customerid }}</td>
<td>{{ budgetauditobject.amount }}</td>
</tr>
{% endfor %}
的.
語法將允許您跟蹤和查詢鏈接表中的項目。 budgetauditobject.bugettypeid.budgettype
表示“獲取與此budgetauditobject對應的bugettypid的bugettype字段值”
你可以嘗試改變
{{ budgetauditobject.budgettypeid }}
至
{{ budgetauditobject.budgettypeid.budgettype }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.