Skip to content

Instantly share code, notes, and snippets.



Forked from seanbehan/
Last active Jan 4, 2021
What would you like to do?
Flask with Django ORM
Located under app/
import os
from django.apps import apps
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
#!/usr/bin/env python
import os, sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from import execute_from_command_line
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)
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/
mv app/
mv app/
python makemigrations
python migrate
visit http://localhost:5000
See for
from flask import Flask
from app.models import Hit
app = Flask(__name__)
def index():
return str(Hit.objects.count())
if __name__=='__main__':
import dj_database_url
DATABASES = { 'default': dj_database_url.config(default='postgres://localhost/py_example_app') }
INSTALLED_APPS = ( 'app', )

This comment has been minimized.

Copy link
Owner Author

@ChaosJohn ChaosJohn commented Jul 24, 2020

Forked from seanbehan/ (Thank seanbehan for the solution. @seanbehan)
Move initialization code for DjangoORM from main file to app/


This comment has been minimized.

Copy link

@avrahamkam avrahamkam commented Oct 27, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment