def generate_tags(n): tags = list() for i in range(n): tag = Tag() tag.title = faker.color_name() tags.append(tag) try: db.session.add(tag) db.session.commit() except Exception as e: log.error("Fail to add tag %s: %s" % (str(tag), e)) db.session.rollback() return tags
def add_tags_to_post(post, tags_list): for item in tags_list: tag = Tag.query.filter_by(title=item).first() if tag: post.tags.append(tag) else: new_tag = Tag(item) post.tags.append(new_tag)
def add_tags_to_post(post, tags_list): for item in tags_list: tag = Tag.query.filter_by(title=item).first() # Add the tag if it exists. If not, make a new tag if tag: post.tags.append(tag) else: new_tag = Tag(item) post.tags.append(new_tag)
def generate_tags(): titles = ["Python", "Flask", "Webdev"] for i in range(TAG_COUNT): tag = Tag(title=titles[i]) db.session.add(tag) db.session.commit()