-
-
Save ChaosJohn/3842014035353d11591ec82f885eeebf to your computer and use it in GitHub Desktop.
Flask with Django ORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Located under app/ | |
""" | |
import os | |
from django.apps import apps | |
from django.conf import settings | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") | |
apps.populate(settings.INSTALLED_APPS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, sys | |
if __name__ == "__main__": | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") | |
from django.core.management import execute_from_command_line | |
execute_from_command_line(sys.argv) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from django.contrib.postgres.fields import JSONField | |
class BaseModel(models.Model): | |
created_at = models.DateTimeField(auto_now_add=True, null=True) | |
updated_at = models.DateTimeField(auto_now=True, null=True) | |
def attributes(self): | |
return self.__dict__ | |
class Meta: | |
abstract = True | |
class Hit(BaseModel): | |
data = JSONField(null=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run the following commands (bc. gists don't allow directories) | |
pip install flask django dj-database-url psycopg2-binary | |
mkdir -p app/migrations | |
touch app/migrations/__init__.py | |
mv models.py app/ | |
mv __init_.py app/ | |
python manage.py makemigrations | |
python manage.py migrate | |
python server.py | |
visit http://localhost:5000 | |
See https://docs.djangoproject.com/en/1.11/topics/settings/#either-configure-or-django-settings-module-is-required for | |
documentation | |
""" | |
from flask import Flask | |
from app.models import Hit | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
Hit.objects.create(data='{}') | |
return str(Hit.objects.count()) | |
if __name__=='__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dj_database_url | |
DATABASES = { 'default': dj_database_url.config(default='postgres://localhost/py_example_app') } | |
INSTALLED_APPS = ( 'app', ) | |
SECRET_KEY = 'REPLACE_ME' |
@ChaosJohn Have you run into any issues of unclosed threads?
https://gist.github.com/seanbehan/168a277a5a23086e76000a59e54fb3d5#gistcomment-3300860
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forked from
seanbehan/app.py
. (Thank seanbehan for the solution. @seanbehan)Move initialization code for DjangoORM from main file to
app/__init__.py