Django外键关系和模板中的显示

时间:2016-09-01 05:29:36

标签: django django-models django-templates django-views

我正在尝试检索与PartyID相关的FirstName和LastName字段,PartyID是我的ToolTransaction模型上的外键。我有一个mysql数据库,我用它来存储数据。我的目标是让名称显示在与PartyID相对应的相应事务中。我已经能够显示所有活动的交易,但我也无法使用它们显示名字和姓氏。我的一些代码在尝试解决问题时可能有点不知所措,所以我道歉。

模特:

class Tool(models.Model):
ToolID=models.CharField(max_length=100, primary_key = True, unique=True, db_column=b'ToolID')
Quantity=models.IntegerField(null=False)
Location=models.CharField(max_length=100, null=False)
CategoryID=models.IntegerField(null=True)
Deleted=models.BooleanField(default=0)

objects=models.Manager()

class Meta:
        managed=True
        db_table='Tool'

def __unicode__(self):
    return self.ToolID

def get_absolute_url(self):
    return reverse("ToolSearch:toolSearchResults", kwargs={"pk": self.ToolID})

class Party(models.Model):
PartyID=models.AutoField(primary_key=True, db_column=b'PartyID')
FirstName=models.CharField(max_length=100, null=False)
LastName=models.CharField(max_length=100, null=False)
PhoneNumber=models.CharField(max_length=25, null=False)
Organization=models.CharField(max_length=100, null=True)
Deleted=models.BooleanField(default=0)

objects=models.Manager()

class Meta:
    managed=True
    db_table='Party'

def __unicode__(self):
    return str(self.PartyID)

def get_absolute_url(self):
    return reverse("ToolSearch:borrowerUpdate", kwargs={"pk": self.PartyID})

class ToolTransaction(models.Model):
CheckOutID=models.AutoField(primary_key=True)
ToolID=models.ForeignKey(Tool, db_column='ToolID', on_delete=models.CASCADE, related_name='ToolID_fk',)
PartyID=models.ForeignKey(Party, db_column='PartyID', on_delete=models.CASCADE, related_name='PartyID_fk',)
Quantity=models.IntegerField(null=False)
CheckOutDate=models.DateField(null=False, default=datetime.datetime.now)
CheckInDate=models.DateField(null=True)
Deleted=models.BooleanField(default=0)
objects=ActiveTransactionManager()
#objects=models.Manager()

class Meta: 
    managed=True
    db_table='ToolTransaction'
    ordering=('CheckOutID',)

def __unicode__(self):
    return str(self.CheckOutID)

def get_absolute_url(self):
    return reverse("ToolSearch:toolCheckin", kwargs={"pk": self.CheckOutID})

查看:

def tool_transaction_search(request):
instance=ToolTransaction.objects.all()
ToolTransaction.objects.filter(PartyID=instance.PartyID).select_related()
    context={
        "Transactions": instance,
    }
    return render(request, "tooltransactionsearch.html", context)

模板:

{% for tran in Transactions %}
            <h5>
                CheckOutID: <a href="{{ tran.get_absolute_url }}">  {{tran.CheckOutID}} </a> <br>
                Name: {{tran.Party.FirstName}} {{tran.LastName}} <br>
                PartyID: {{tran.PartyID}} <br>
                ToolID: {{tran.ToolID}} <br>
                CheckOutDate: {{tran.CheckOutDate}} <br>
                CheckInDate: {{tran.CheckInDate}} <br> <br>
            </h5>
            {% endfor %}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

类ToolTransaction中的PartyID属性实际上是Party的一个实例,因此您可以通过这种方式获取FirstName和LastName:

Name: {{tran.PartyID.FirstName}} {{tran.PartyID.LastName}}