def new_blog(request: schemas.Blog, db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_user)): new_blog = models.Blog(title=request.title, body=request.body, author_id=1) db.add(new_blog) db.commit() db.refresh(new_blog) return {"data_recieved": new_blog}
def test_valid(self): """正常な入力を行えばエラーにならないことを検証""" params = dict(content="test", photo="", anime_id="", anime="", tag="tag,tag、tag", user=1) blog = models.Blog() form = forms.BlogForm(params, instance=blog) self.assertTrue(form.is_valid())
def create(request: schemas.Blog, db: Session): new_blog = models.Blog(title=request.title, body=request.body, user_id=1) db.add(new_blog) db.commit() db.refresh(new_blog) return new_blog
def add_blog(member_id, title, content): blog = models.Blog(member_id=member_id, title=title, content=content) blog.save() return blog
def create_user(db: Session, title, body): new_blog = models.Blog(title=title, body=body, user_id=1) db.add(new_blog) db.commit() db.refresh(new_blog) return new_blog
if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_add_rbac.settings") import django django.setup() # 插入数据 from blog import models # 往博客表中插入数据 blog_lst = ['火之国','水之国','风之国','雷之国','土之国'] name_lst = ['whw','wanghw','naruto','sasuke','madara'] b_lst = [] for bid,blog in enumerate(blog_lst): blog_obj = models.Blog(name=blog,user=name_lst[bid]) b_lst.append(blog_obj) models.Blog.objects.bulk_create(b_lst) # 往文章表中插入数据 category_choices = [4,2,3] dates = ['2011-12-12','2012-3-5','2018-12-5'] blog_id_lst = [1,2,3,4,5] lst = [] # 插入50个数据 for i in range(50): random_title = ''.join(random.choices(string.ascii_letters, k=5)) random_category = random.choice(category_choices) random_content = ''.join(random.choices(string.ascii_letters, k=9)) random_date = random.choice(dates) random_blog = random.choice(blog_id_lst)
def test_either1(self): """何も入力しなければエラーになることを検証""" params = dict() blog = models.Blog() form = forms.BlogForm(params, instance=blog) self.assertFalse(form.is_valid())