modify the asyncorm example, for the new lazy querysets

This commit is contained in:
monobot 2017-05-11 18:15:04 +01:00
parent 861865807a
commit ad68739df7
2 changed files with 12 additions and 13 deletions

View File

@ -1,5 +1,5 @@
from sanic import Sanic from sanic import Sanic
from sanic.exceptions import NotFound from sanic.exceptions import NotFound, URLBuildError
from sanic.response import json from sanic.response import json
from sanic.views import HTTPMethodView from sanic.views import HTTPMethodView
@ -46,21 +46,21 @@ def ignore_404s(request, exception):
# now the propper sanic workflow # now the propper sanic workflow
class BooksView(HTTPMethodView): class BooksView(HTTPMethodView):
def arg_parser(self, request):
parsed_args = {}
for k, v in request.args.items():
parsed_args[k] = v[0]
return parsed_args
async def get(self, request): async def get(self, request):
filtered_by = self.arg_parser(request) filtered_by = request.raw_args
if filtered_by: if filtered_by:
q_books = await Book.objects.filter(**filtered_by) try:
q_books = Book.objects.filter(**filtered_by)
except AttributeError as e:
raise URLBuildError(e.args[0])
else: else:
q_books = await Book.objects.all() q_books = Book.objects.all()
books = [BookSerializer.serialize(book) for book in q_books] books = []
async for book in q_books:
books.append(BookSerializer.serialize(book))
return json({'method': request.method, return json({'method': request.method,
'status': 200, 'status': 200,
@ -80,7 +80,6 @@ class BooksView(HTTPMethodView):
'results': BookSerializer.serialize(book), 'results': BookSerializer.serialize(book),
}) })
class BookView(HTTPMethodView): class BookView(HTTPMethodView):
async def get_object(self, request, book_id): async def get_object(self, request, book_id):
try: try:

View File

@ -1,2 +1,2 @@
asyncorm==0.0.7 asyncorm==0.0.9
sanic==0.4.1 sanic==0.5.4