Q:

Join two models together using a common column

I have two tables
Table 1
CREATE TABLE `profiles` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `first_name` varchar(45) DEFAULT NULL,
    `surname` varchar(45) DEFAULT NULL,
    `email` varchar(45) DEFAULT NULL,
    `unique_id` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

Table 2
CREATE TABLE `bible_photo` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `file` varchar(100) NOT NULL,
    `file_name` varchar(45) DEFAULT NULL,
    `unique_id` varchar(100) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

And two corresponding models
Models
class TProfiles(models.Model):
    id = models.IntegerField(primary_key=True)  # AutoField?
    first_name = models.CharField(max_length=45, blank=True)
    surname = models.CharField(max_length=45, blank=True)
    email = models.CharField(max_length=45, blank=True)
    unique_id = models.CharField(max_length=100, blank=True)

    class Meta:
        managed = False
        db_table = 'profiles'

class Photo( models.Model ):
    file = models.FileField( upload_to = settings.MEDIA_ROOT )
    file_name = models.CharField(max_length=45, blank=True)
    unique_id = models.CharField(max_length=100, blank=True)

    def extension(self):
        name, extension = os.path.splitext(self.file.name)
        return extension

I want to access all profiles and their corresponding profile picture, based on the unique ID they both share. I have the following in my views.py file. However, although I can access them both individually, I cannot combine them to pass to the template.
views.py
def register(request):
    photos = Photo.objects.all
    profiles = TProfiles.objects.all

    return render_to_response("register.html", {
        "form": form, "photos": photos,
    }, RequestContext(request))  

Update
I tried adding profile = models.ForeignKey(TProfiles) to my Photo model, but I keep getting complaints about missing columns relating to the Profiles table. Surely I don't have to duplicate them in to Photos table?
OperationalError at /register/
(1054, "Unknown column 'bible_photo.profile_id' in 'field list'")

And this is the line of code in my template that it is moaning about. I am trying to see if I can access the profile from the photo.
{% for p in photos %}
    <h1>{{ p.profile }}</h1>
{% endfor %}

A:

change your models.py like this:
class TProfiles(models.Model):
    first_name = models.CharField(max_length=45, blank=True)
    surname = models.CharField(max_length=45, blank=True)
    email = models.CharField(max_length=45, blank=True)
    unique_id = models.CharField(max_length=100, blank=True)

    class Meta:
        managed = False
        db_table = 'profiles'

class Photo( models.Model ):
    file = models.FileField( upload_to = settings.MEDIA_ROOT )
    file_name = models.CharField(max_length=45, blank=True)
    unique_id = models.CharField(max_length=100, blank=True)
    event_location_time = models.ForeignKey(TProfiles)      

    def extension(self):
        name, extension = os.path.splitext(self.file.name)
        return extension

And change your view like this:
def register(request):
    profiles = TProfiles.objects.all()

    return render_to_response("register.html", {
        "form": form, "profiles": profiles,
    }, RequestContext(request))  

And in your template you can use :
{% for profile in profiles %}
    #access to profile object
    {% for photo in profile.photo_set.all %}
     #access to each profile photo object
    {% endfor %} 
{% endfor %}

