def create_author(user_id: int, args: dict): author = Author(**args) db.session.add(author) db.session.commit() return jsonify({'success': True, 'data': author_schema.dump(author)}), 201
def create_author(user_id: int, args: dict): author = Author(**args) # data = request.get_json() # first_name = data.get('first_name') # last_name = data.get('last_name') # birth_date = data.get('birth_date') # author = Author(first_name=first_name, last_name=last_name, birth_date=birth_date) db.session.add(author) db.session.commit() # print(author) return jsonify({'success': True, 'data': author_schema.dump(author)}), 201
def add_data(): """Add sample data to database.""" try: data_json = load_json_data('authors.json') for item in data_json: item['birth_date'] = datetime.strptime(item['birth_date'], '%d-%m-%Y').date() author = Author(**item) db.session.add(author) data_json = load_json_data('books.json') for item in data_json: book = Book(**item) db.session.add(book) db.session.commit() print('Data has been successfully added to the database.') except Exception as exc: print('Unexpected error: {}'.format(exc))