Skip to content

Instantly share code, notes, and snippets.

View menecio's full-sized avatar
🐍

Aristóbulo Meneses menecio

🐍
View GitHub Profile
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Movie
def movies_list(request):
queryset = Movie.objects.all()
content = serialize('json', queryset)
@menecio
menecio / mymoviedb-utils.py
Created September 7, 2019 10:17
database_sync_to_async borrows from channels
from django.db import close_old_connections
from asgiref.sync import SyncToAsync
class DatabaseSyncToAsync(SyncToAsync):
"""
SyncToAsync version that cleans up old database connections when it exits.
"""
from aiohttp import web
from django.apps import apps
from django.core.serializers import serialize
from mymoviedb.utils import database_sync_to_async
routes = web.RouteTableDef()
@routes.view('/movies')
from aiohttp import web
from django import setup
from django.conf import settings
from mymoviedb import settings as my_settings # not the same as django.conf.settings
from movies.routes import movies_app
async def setup_django(app):
'''
@menecio
menecio / mymoviedb-models.py
Last active September 7, 2019 09:29
Our Movie model
from django.db import models
class Movie(models.Model):
release_date = models.DateField()
title = models.CharField(max_length=512)
rating = models.PositiveSmallIntegerField()
duration = models.DurationField()
director = models.CharField(max_length=512)
genre = models.CharField(max_length=16)