I looked at my code some more, and found a now obvious mistake. I changed
my code to:
def edit(id, tags=None):
edit_post = Post.query.get(id)
if tags is not None and tags != "":
for tag in tags.split(','):
tag = tag.strip(" ")
if tag not in edit_post.tags:
add_tags = Tag()
add_tags.text = tag
edit_post.tags_relationship.append(add_tags)
db.session.add(add_tags)
db.session.commit()
for tag in edit_post.tags:
if tag not in tags:
edit_post.tags.remove(tag)
db.session.commit()
else:
for tag in edit_post.tags:
edit_post.tags.remove(tag)
db.session.commit()
However, now if I remove all the tags from a post, a last one remains, and
I have to remove it again to get rid of all the tags.
On Monday, February 23, 2015 at 1:46:07 AM UTC+5:30, Asad Dhamani wrote:
>
> I have a many-to-many relationship in my models. This is what they look
> like:
>
> class Post(db.Model):
> __tablename__ = 'Posts'
> id = db.Column(db.Integer, primary_key=True)
> tags_relationship = db.relationship('Tag', secondary=tags, backref=db.
> backref('posts', lazy='dynamic'))
> tags = association_proxy('tags_relationship', 'text')
> full_text = db.Column(db.Text, nullable=True)
> fulltext_vector = db.Column(TSVectorType('full_text'))
>
>
>
> class Tag(db.Model):
> id = db.Column(db.Integer, primary_key=True)
> text = db.Column(db.String(255))
>
>
> tags = db.Table('tags',
> db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
> db.Column('post', db.Integer, db.ForeignKey('Posts.id'))
> )
>
> I need to add the ability to edit tags. I know how to add tags:
>
> def edit(id, tags=None):
> edit_post = Post.query.get(id)
> if tags is not None and tags != "":
> for tag in tags.split(','):
> tag = tag.strip(" ")
> if tag not in edit_post.tags:
> add_tags = Tag()
> add_tags.text = tag
> edit_post.tags_relationship.append(add_tags)
> db.session.add(add_tags)
> db.session.commit()
>
> This is what I'm doing for removing tags:
>
> for tag in edit_post.tags:
> if tag not in tags:
> edit_post.tags.remove(tag)
> db.session.commit()
>
> However, I can not delete the last remaining tag from a post. Once I add a
> tag, it seems like there has to be a minimum of 1 tag on that post, as I
> can add more, and I can remove all but the last remaining tag.
>
> What am I doing wrong?
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.