Bussiere
Bussiere

Reputation: 1152

web2py how to make a list:reference of an other table in db.py

I have an app tag and an app image.

How do I make a reference from image to the app tag database?

tag app db.py :

db.define_table('Tag',
   Field('Name', unique=True),
   format = '%(Name)s')

image app db.py :

db.define_table('Image',
   Field('Nom'),
   Field('Date_Creation',date),
   Field('Tag','list:reference Tag'),
   format = '%(Nom)s')

Upvotes: 1

Views: 1535

Answers (1)

profitehlolz
profitehlolz

Reputation: 640

I think you're looking for many-to-many relationship; images can have many tags, tags can belong to many images.

So you would need another table to store this relationship between the two.

db.define_table('image_tag',
    Field('image', db.Image),
    Field('tag', db.Tag))

To get images and their tags:

images_tags = db((db.Image.id==db.image_tag.image) & (db.Tag.id==db.image_tag.tag))
for row in images_tags.select():
    print row.Image.Nom, row.Tag.Name

Upvotes: 1

Related Questions