REST API With Flask & SQL Alchemy

preview_player
Показать описание
In this project we will use Python Flask along with SQL Alchemy and Marshmallow to create a RESTful API for products.

Sponsor: Freelancer Bundle (Use "brad25" for 25% off)

Code:

Flask From Scratch Series:

💖 Become a Patron: Show support & get perks!

Website & Udemy Courses

Follow Traversy Media:
Рекомендации по теме
Комментарии
Автор

0:27 - Intro to tutorial
1:00 - Intro to the other packages used in this tutorial (SQLAlchemy, Marshmallow, PostMan)
2:01 - Creating virtual environment (Pipenv)
3:09 - Installing dependencies
4:10 - Create main file (app.py)
5:43 - Initialize flask and run server
7:02 - Creating a basic route
7:58 - Making POST request in Postman
8:18 - Setting up database URI
9:04 - Setting up database
10:46 - Initialize database
11:00 - Initialize Marshmallow
11:23 - Creating a class for your resource(s)
16:13 - Creating a product schema (This is where we use Marshmallow)
17:13 - Initialize schema
17:56 - Another schema initialization
18:50 - Creating the database
20:04 - Creating our routes
20:15 - "Create a product" route ('/product')
23:30 - Making a POST request in Postman
24:50 - Creating "fetch all products" route
25:40 - SQLAlchemy .all() method
26:46 - Testing get all products in Postman
27:28 - Creating "get single product" route
29:29 - Creating "update a product" route
31:35 - Making a PUT request to product in Postman
32:21 - Create a delete route
33:49 - Making a DELETE request in Postman

cannon
Автор

This channel is the gift that keeps giving.

imbayi
Автор

Breaking Marshmallow changes since the version used to record this tutorial.

3.0.0b7 (2018-02-03)
Features:

Backwards-incompatible: Schemas are always strict (#377). The strict parameter is removed.

Backwards-incompatible: Schema().load and Schema().dump return data instead of a (data, errors) tuple (#598).

JohnLednicky
Автор

If when running "db.create_all()" from the Python interpreter you experience "RuntimeError: Working outside of application context.", instead run the following three lines:
>>> from app import app, db
>>> app.app_context().push()
>>> db.create_all()

MoltiSanti
Автор

Another great video thank you.

If anyone gets the error "AttributeError: 'list' object has no attribute 'data'", it's because since Marshmallow v3 you don't need the .data attribute as dump returns the data directly ...

def get_products():
all_products = Product.query.all()
result =

return jsonify(result)

SimonMarkHolland
Автор

Just for someone who gets stuck when they get all products, the "result" variable will be default be an array/list so you don't need the `data` you can just jsonify(result)

MaxProgramming
Автор

For people coming across this video in June 2021. I had a few issues using the latest versions of everything.

1. At 18 minutes the schema no longer requires strict = True.
2. At 16.30, result.data should be result.

ruudhermans
Автор

Thank you for presenting an example by using my favorite two tools - pipenv and vscode. They save so much time and effort. I also ALWAYS create a git repository for projects, even if they are the throwaway kind. As of this date, marshmallow is version 3.0 and the "strict" key is no longer needed or allowed.

johncrunk
Автор

For anyone using this now, and update to Marshmallow (from documentation):
Setting the strict option on class Meta has no effect on Schema behavior. Passing strict=True or strict=False to the Schema constructor will raise a TypeError.

nickapeed
Автор

Hi Traversy, Thanks so much for this video. I had been reading a lot and trying to get the concept of such an API but it was confusing to me mostly because many tutorials start off by putting different things in different files and then they import different modules into each other-it's just too confusing for a beginner. So thanks to God you put everything in a single file and gave us an overview. You have no idea how that is important to me.

Ambrozekwerondaa
Автор

For anyone confused about how the table name is generated, I just realized that on executing the db.create_all() command, a table with same name as the lower cased Class name gets created in SQLite database, so in this case a table named 'product' is created with the defined schema. Also, if Class name has a name like MyProduct then table would be named as my_product.

chinmoyjyotikathar
Автор

Thank you! I’m coming from node web backends and just tryna switch it up and start using python instead. Most tutorials were just brushing over the basics without explaining and getting straight to the point. Thank you brad, you’re THE best!

zb
Автор

Thanks for the video Brad. kudos.

I was getting this error when sending a GET to return all the Products,
To fix it I returned jsonify(result) instead of jsonify(result.data)

I hope it will save someone else time
=)

ua
Автор

Quite old, some methods are not the same right now. But it's definitely a good tutorial.

The strict param is not accepted right now for marshmallow (product(s)_schema)
For the get_all the following lines are the new ones:
all_products = Product.query.all()
return

bureaucafeatelier
Автор

Still useful in 2022. Thanks so much for this tutorial. I can't believe how easy this was to implement.

MegaCookiecook
Автор

Awesome tutorial, it clearly shows why Flask is the best framework for python API development.
You can also use Flask-Restful to define your endpoints.
It give you all the HTTP verbs ready, your endpoints will be called resources rather than routes.

Thanks Brad for your hard work and dedication.

mahmoudtokura
Автор

Flask is ridiculously simple to work with Rest APIs. So grateful with this tutorial

k.santiagodiaz
Автор

Thank you Brad, i learn anything from node js, react js, and today currently learn python(flask) from you. I prefer with how you teach some thing

IGEDEWICAKSANA
Автор

Thank you so much for this.
As a part of an interview assignment, I was given a task to create a CRUD system and expose APIs for a table.
Starting from absolutely 0 knowledge about Flask, I was able to fully develop it thanks to you.
Thank you so much!

Do let me know if there is some way I can buy you a couple of beers (your Paypal?)

askmehow
Автор

Hi, am getting an error, (TypeError: __init__() got an unexpected keyword argument 'strict') any advice

takich
join shbcf.ru