furiousGeorge
furiousGeorge

Reputation: 7

Django Foreign Key Relation and Display In Template

I am trying to retrieve the FirstName and LastName fields related to a PartyID which is a foreign key on my ToolTransaction model. I have a mysql database which I am using to store the data. My objective is to have the names appear with the appropriate transaction as opposed to the PartyID. I have been able to have all the active transactions display but I am unable to have the First and Last Name show with them as well. Some of my code may be a bit wonky from me trying to troubleshoot the issue, so my apologies.

Models:

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

View:

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)

Template:

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

Any help will be greatly appreciated!

Upvotes: 0

Views: 1381

Answers (1)

LostInThought
LostInThought

Reputation: 61

the PartyID attribute in class ToolTransaction is actually an instance of Party, so you can get FirstName and LastName in this way:

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

Upvotes: 2

Related Questions