better POST example

This commit is contained in:
narzeja 2016-10-22 08:50:56 +02:00
parent c3628407eb
commit b048f1bad3

View File

@ -49,16 +49,20 @@ objects.database.allow_sync = False # this will raise AssertionError on ANY sync
app = Sanic('peewee_example') app = Sanic('peewee_example')
@app.route('/post') @app.route('/post/<key>/<value>')
async def post(request): async def post(request, key, value):
""" This actually requires a GET request, you probably want POST in real """
life, with some data parameters""" Save get parameters to database
obj = await objects.create(KeyValue, key='my_first_async_db', text="I was inserted asynchronously!") """
obj = await objects.create(KeyValue, key=key, text=value)
return json({'object_id': obj.id}) return json({'object_id': obj.id})
@app.route('/get') @app.route('/get')
async def get(request): async def get(request):
"""
Load all objects from database
"""
all_objects = await objects.execute(KeyValue.select()) all_objects = await objects.execute(KeyValue.select())
serialized_obj = [] serialized_obj = []
for obj in all_objects: for obj in all_objects: