Django - 外键不工作

时间:2014-04-24 08:06:58

标签: python sql django django-templates

我试图使用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(例如费用)?

这是我的观点&amp;模板

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'

5 个答案:

答案 0 :(得分:1)

在模板中尝试:

{% 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关系代表另一个实体。

有关关系查找,请参阅this link

答案 1 :(得分:1)

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 %}

答案 2 :(得分:0)

{{ budgetauditobject.budgettypeid.budgettype }}

答案 3 :(得分:0)

非常简单,只需follow the relationship,就像这样:

{% 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字段值”

答案 4 :(得分:0)

您可以尝试更改

{{ budgetauditobject.budgettypeid }}

{{ budgetauditobject.budgettypeid.budgettype }}