1
It seems that Rfid.tag_id is really a foreign key to RfidNames.badge_id. You should declare it as such: a foreign key field can just as easily be a char field.
class Rfid(models.Model):
...
tag = models.ForeignKey('RfidNames', db_column='Tag_ID', to_field='badge_id', blank=True, null=True)
Now you can simply follow the relationship in the template:
{% for scan in scan_list %}
{{ scan.site_id }} {{ scan.tag.name }} {{ scan.time }}
{% endfor %}
That incurs a db lookup per row, so you can use select_related
in the view to make it significantly more efficient:
scan_list = Rfid.objects.all().using('devices').order_by('-time').select_related('RfidNames')
Note you don’t need the explicit step of converting to a list, in any case.
Source:stackexchange.com