I used the answer to this question to fix a circular import when setting up Flask-SQLAlchemy. The solution works because you don't have to import the app from the application.py file to get the database URI - it initalizes the connection just before running the app. The issue now is that I typically like import my models file into a python interpreter just so I can test the models (and the connection to the database) without actually running the app. But with this setup, just importing models won't establish a connection to PostGres.
Is there a way to establish a connection to PostGres through Flask-SQLAlchemy without actually initializing and running an app? Maybe I can provide db with the URI to my database directly?
Study this code
https://github.com/mattupstate/overholt
there is a link on github explaining the design. What should be your main focus is the usage of flask-script. I think you have to use the factory pattern, but you want to do that anyway if you are doing anything remotly serious.
Best of luck
It looks like you are searching for this one github.com/mardix/active-alchemy
Active-Alchemy is a framework agnostic wrapper for SQLAlchemy that makes it really easy to use by implementing a simple active record like api, while it still uses the db.session underneath. Inspired by Flask-SQLAlchemy
Related
I have written a web app in django using mongoengine as a driver and am trying to host my database on mongodb cloud. In mongoengine's connect() method, according to what I understood from the documentation, specifying the url (specified in the connect section on the mongo cloud site) in the hostname argument should do the trick, however requests from the app to the mongo cloud keep timing out.
Where am I going wrong?
This is the call to the connect method in my setting.py file-
mongoengine.connect(db='scheduler', host='mongodb+srv://<user>:<password>#cluster0.oualt.mongodb.net/<db name>?retryWrites=true&w=majority')
Update: the specific error thrown on using the above url is this-
ServerSelectionTimeoutError
If you're using just mongoengine then Django will not detect that connect to the database, so you will not be able to use the Django ORM system to save instances of models etc.
If you want to use the ORM you would have to use a package like Djongo or djmongoengine
However, if you're not planning to use the ORM system then you can use just mongoengine.
I believe the connect url should be in this format:
mongodb+srv://<username>:<password>#<database-name>.mongodb.net/test?retryWrites=true&w=majority
Right now you seem to have the url to a specific cluster on your database.
So I was going through similar posts which had the same error ServerSelectionTimeoutError with djongo and pymongo as the drivers and one of the posts said that they had forgotten to whitelist their IP address on the mongo cloud site. I had done that during signup, but I repeated it anyways and for some reason the IP address was different and after that had been whitelisted, my app connected and started working. I have no explanation.
mongoengine works fine, but in terms of protocols you need another ext file that serves for your connection protocols, just pip install pymongo[srv]
def register_database(db_name:str):
connect(db=f"mongodb+srv://<username>:<password>#<cluster-name>.nwr5l.mongodb.net/{db_name}?retryWrites=true&w=majority", alias="default")
I am new to Flask app development, i need some clarifications on using database with flask app.
We have a MySQL db which has some metrics, currently we generate some reports using python using MySQL.connector.
We have python modules/functions to fetch the data from the db using mysql.connector and populate a dictionary which has data to be put in the report.
My question is can i use the same python module in the app.py (if this is where i create the app) and get the data as dictionary and pass it to some template html to render the report?
If i can do this, what is the advantage of using Flask-MySql or Flask-SqlAlchemy and doing the app.config[] things which are mentioned in many tutorials?
I am trying to understand what should be used when.
well for ur case if ur have not much experience in sql i prefer u to use orm like Sqlalchemy. for flask there is an extension call flask-sqlalchemy.it uses python like syntax without using direct sql it is very easy to learn and well documented.but not recommended for advanced user cases.If u want to pass pure sql queries through flask and to have more capablities in database site better to use flask extension for mysql flask-mysql.It depend on ur requirement,capability and user case.
I have a web app that I developed while learning back-end web development. I deployed it a while back and ended up getting some real users registered. Unfortunately, I wrote some smelly code and it is becoming very difficult to implement any new features without having to refactor large portions of the code base, so I am planning to rewrite the back-end.
The main issue I'm facing is whether there is a way to migrate the CustomUser model I've created to a new PostgreSQL database. Is this possible by simply recreating the same model in the new backend, then doing a pg_dump and restore?
If you don't have too many users, and you are using an identical model, it might be easiest to use the dumpdata management command on your old system, and use the resulting fixture to loaddata on your new system.
I have a running django-nonrel application and I would like to add a real time module using web sockets.
I would like to use django-socketio but it seems that gevent (which is used to serve the site) does not work properly with mongodb (link) which is the backend of the Django application.
Does anyone already try to set up django-socketio with mongodb as a backend ?
Dry
I found that using Tornado/Tornadio2 with Django is much easier than the gevent-based alternatives. If you need help setting that up I'd be happy to share my runserver_socketio.py script.
I am looking for a guide to migrate Django project to Google App Engine and use Google's datastore. The most of the guides I found were linked to Django-Appengine using Django-nonrel (but I want to use GAE's native support).
Going through GAE getting started guide, it says:
Google App Engine supports any framework written in pure Python that speaks CGI (and any WSGI-compliant framework using a CGI adaptor), including Django, CherryPy, Pylons, web.py, and web2py. You can bundle a framework of your choosing with your application code by copying its code into your application directory.
I understand that I won't be able to use some features of Django in that case (majorly the admin feature) and would also need to restructure the models.
From other reading, I also found that latest SDK of GAE now includes Django 1.3 on Python 2.5.
I tried to put all files from my Django application to a GAE project, but couldn't get it all to work together.
Please provide some basic guide using which I may migrate my Django project to Google App Engine's code.
Thanks.
For an existing Django app, using django-nonrel is the simplest approach; it is very popular so you should be able to find help with specific errors you get quickly.
Another approach is written up in this article: http://code.google.com/appengine/articles/pure_django.html -- it goes the other way, taking an App Engine app that uses Django for dispatch, templates, and forms, but not for models, and describes how to make it run in a native Django environment. Maybe you can glean some useful hints for your situation from it.
I've used django-nonrel, which behaves pretty much like django, except that operations with JOINs will return errors. I've basically worked around this by avoiding ManyToMany fields, and essentially building that functionality manually with an intermediate table.
So far I've ran into two problems with Django-nonrel:
1. No access to ancestor queries, which can be run in a transaction. There's a pending pull request for this feature though.
2. You can't specify fields that are not indexed. This could significantly increase your write costs. I have an idea to fix this, but I haven't done so yet.
(Edit: You CAN specify fields that are not indexed, and I've verified this works well).
2 (new). Google is pushing a new database backend called ndb that does automatic caching and batching, which will not be available with django-nonrel.
If you decide not to use django-nonrel, the main differences are that Django models do not run under App Engine. You'll have to rewrite your models to inherit from App Engine's db.Model. Your forms that use Django's ModelForm will need to inherit from google.appengine.ext.db.djangoforms instead. Once you're on App Engine, you'd have to port back Django if you ever take your app somewher else.
If you already have a Django application you might want to check this out. You won't work with App Engine's datastore but Google Cloud SQL might fit your needs.