def generate_posts(n, users, tags): for i in range(n): post = Post() post.title = faker.sentence() post.text = faker.text(max_nb_chars=1000) post.publish_date = faker.date_this_century(before_today=True, after_today=False) post.user_id = users[random.randrange(0, len(users))].id post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: log.error("Fail to add post %s: %s" % (str(post), e)) db.session.rollback()
def generate_posts(n, users, tags): print(tags) for i in range(n): post = Post() post.title = fake.sentence() post.text = fake.text(max_nb_chars=1000) post.publish_date = fake.date_this_century(before_today=True, after_today=False) post.emp_id = random.randrange(1, 100) post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: print(f" The exception is {e}")
import random import datetime from main import Tag, User, Post, db user = User.query.get(1) taglist = [Tag("Python"), Tag('Flask'), Tag("SQLAlchemy"), Tag('jinja')] s = "Example text" for i in range(100): new_post = Post("Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(taglist, random.randint(1, 3)) db.session.add(new_post) db.session.commit()