I am trying to learn Python/Flask/SQLAlchemy by building a simple Wiki (heavily based off of a Flask-Admin example) but am struggling to understand how to get a new column from my many-to-many relationship to display.
I have successfully created the Wiki and have created a many-to-many relationship table for tags with no problem (and tagging works properly as far as I have seen), but I want to display the tags as a column and can't get the logic worked out.
GOAL: I want to display a column that shows the tags that are referenced by the many-to-many association table.
Here is a picture of what I am trying to accomplish:
Here is what I believe to be the relevant code:
wiki_tags_table = db.Table('wiki_tags', db.Model.metadata,
db.Column('wiki_id', db.Integer, db.ForeignKey('wiki.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'))
)
class Wiki(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), unique=True)
description = db.Column(db.Text)
path = db.Column(db.Unicode(256))
date_added = db.Column(db.DateTime)
tags_id = db.Column(db.Integer, db.ForeignKey('tag.id'))
tags = db.relationship('Tag', secondary=wiki_tags_table, backref=db.backref('wiki_tags_table', lazy='dynamic'))
def __unicode__(self):
return self.item
class WikiAdmin(sqla.ModelView):
column_exclude_list = ['path']
column_hide_backrefs = False
form_overrides = {
'path': form.FileUploadField
}
form_args = {
'path': {
'label': 'File',
'base_path': file_path
}
}
column_searchable_list = ('title', 'description', 'path')
def __init__(self, session):
super(WikiAdmin, self).__init__(Wiki, session)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
def __unicode__(self):
return self.name
I have been referencing these documents (mostly trying variations of backref) but haven't figured it out yet: