1

I have the following models:

class Page(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    tags = db.relationship('Tag', secondary=tags, lazy='subquery',
        backref=db.backref('pages', lazy=True))

class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)

tags = db.Table('tags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
    db.Column('page_id', db.Integer, db.ForeignKey('page.id'), primary_key=True)
    db.Column('language', db.String(50), nullable=False)
)

Know if I want to make the following additions:

tag = Tag()
Page.tags.append(tag)

How to set the value for the language column for the new added tag.

1 Answer 1

1

Yes, you can achieve that by creating PageTagModel model.

like the following:

class PageTagModel(db.Model):
    __tablename__ = 'pagetags'

    page_id = db.Column(db.Integer, db.ForeignKey('pages.id'), primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), primary_key=True)

    page = db.relationship('Page', backref=db.backref("pagetags"))
    tag = db.relationship('Tag', backref=db.backref("pagetags"))

    language = db.Column('language', db.String(50), nullable=False)

And please edit the Page model to the following:

class Page(db.Model):
    id = db.Column(db.Integer, primary_key=True) 

I assume you gave the Tag model table this tags by using __tablename__ = 'tags' and Page model to pages.

Full picture:

class Page(db.Model):
    __tablename__ = 'pages'
    id = db.Column(db.Integer, primary_key=True)

class Tag(db.Model):
    __tablename__ = 'tags'
    id = db.Column(db.Integer, primary_key=True)

class PageTagModel(db.Model):
    __tablename__ = 'pagetags'

    page_id = db.Column(db.Integer, db.ForeignKey('pages.id'), primary_key=True)
    tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), primary_key=True)

    page = db.relationship('Page', backref=db.backref("tags_annotation"))
    tag = db.relationship('Tag', backref=db.backref("tags_annotation"))

    language = db.Column('language', db.String(50), nullable=False)

 # to use it
 tag = Tag()
 page = Page()
 page_tags = PageTagModel(page=page, tag=tag, language='English')

 db.session.add(tag)
 db.session.add(page)
 db.session.add(page_tags)
 db.session.commit()

if you want to get tags directly from the Page model then you must add the following line to the Page model

tags = db.relationship('Tag', secondary='pagetags', lazy='subquery', backref=db.backref('pages', lazy=True))
Sign up to request clarification or add additional context in comments.

3 Comments

Then how to use it, to set the language field?
And to get tags of a certain Page object, I should use tags or pagetags?
Sorry for confusing you can remove the tags from Page model. Answered + Code updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.