I try do show an example, if I get the point. Let's say you have these classes:
class User():
def __init__(self, id, name):
self.id = id
self.name = name
class Image():
def __init__(self, id, user_id, filename):
self.id = id
self.user_id = user_id
self.filename = filename
And the following collections:
users = [User(1, 'Jim'), User(2, 'Spock')]
images = [Image(1, 1, 'jim_1.jpg'), Image(2, 1, 'jim_2.jpg'), Image(3, 2, 'spk_1.jpg')]
Once you fetch a user form the collection, let's say the first:
user = users[0]
You can query for images in this way:
user_images = [ image for image in images if image.user_id == user.id ]
for image in user_images:
print(image.filename)
While if you have the image, since in this case is a relation one to many:
image = images[0]
user = [user for user in users if user.id == image.user_id][0] # [0] as it is 1:n relation
For the join table:
join_table = [ {'name': user.name, 'filename': image.filename} for user in users for image in images if user.id == image.user_id ]
for e in join_table:
print(e['name'], e['filename'])
Which returns:
# Jim jim_1.jpg
# Jim jim_2.jpg
# Spock spk_1.jpg