I build 2 flask Apps App1 and App2(Two different services). Both Apps are referring to the same DB. Am using MongoDB as database and MongoEngine to create connections and to support ORM Queries.
I have created a user table in App1 and I defined the structure of the table in models.py file. Now I have to use the same user table in the App2. How would I use the existing table itself without rewriting the same code in APP2?
I can do it in one way that I can write a Mongo wrapper which will connect and serve the data. But I don't want to write RAW queries. Can someone help me how to do this? Thanks!
You can put all your database-related code into a separate Python package which both your applications can then import.
OR
You could also consider building a separate application around your database code that exposes information through an API. Your other applications could then make requests to this API.
Related
I am using Django Rest Framework for backend development, where I have set up 2 environments. I have split settings files as required and both the environments have different DBs which are working fine, I am using Zappa(AWS lambda) for deploys, I was wondering is there an elegant way of migrating data(not models but data inside models say a few config changes) than preparing PostgreSQL DB rollouts, Could not find much on the internet, so asking here.
Please let me know if there are any questions.
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 django application and i've needed to create multiple instances. So currently i create a virtualenv for each instance with its own django project. However this is not scaling well. What i really need is multiple instances of the application inside a single django project.
Is there any sort of examples or advice for something like this? I was thinking of using multiple databases, but then each db would get all of the models for all applications in the project.
I think you would like to see Multi Tenant Applications in Django.
If you follow this book it contain what you are looking for. Building Multi Tenant Applications with Django
Taken from the above shared docs.(Summary)
The various approached to multi tenancy
Shared database with shared schema
Shared database with isolated schema
Isolated database with a shared app server
Completely isolated tenants using Docker
I have a lot of old vbscript webpages using Classis ASP. Those ASP pages have lots of different databases queries to different databases all inside a given .asp file. I want to write those in Python and use Django as the framework. Will I be able to do this? I'm not sure how to start after I install Django. Sure, I can make a demo work, but that's not what I'm after. I will use the normal database "things" in MySQL or PostgreSQL, but sooner or later I have to hit those other databases and bring them back into Django, using Django's templating and so on.
Will I be able to do this? How do I make a model that queries my legacy SQL Server and Oracle database and send that to the view? Am I "fighting the framework" to accomplish this?
Just to be clear. I am not interested in messing with the stock database that Django uses for it's settings. That can stay as it is. I want to use that part for plugins, security (ldap), etc.
Thanks.
You can query different databases in your ORM calls by leveraging the using statement: https://docs.djangoproject.com/en/1.5/ref/models/querysets/#using
This would allow you to set up as many database definitions as you need in settings.py, then specify which DB to query at the view level. That way, you wouldn't have to change your model definition should you decide to consolidate your databases, etc.
Have you reviewed the Django multiple databases documentation?
Django has a built-in Oracle back end, so that should be fairly straightforward.
SQL Server can work through django-pyodbc but I found it fairly painful to set up. If you already have a generally working ODBC connection to your legacy SQL Server database connection from your Django environment it's no big deal, but it took me some trouble to get things set up to where I could use tsql to connect to my SQL Server database.
I also had some trouble with Unicode data from the SQL Server database until I forced it to use the appropriate (later) version of TDS. I just needed it for a script or two, so I set the 'TDSVER' environment variable and left it at that, but in theory there are other places you can set that.
Using Django 1.4 is it possible to have 2 apps within the same project use two different databases?
If so, will each app have there own settings.py?
Have a look at the django documentation on how to define multiple databases in your settings.
To use a different database for each app you can use one of to approaches:
You can manually select a database on each database call. This will probably not be possible if you used third party apps.
You can define a database router which is probably more suitable if you do not want to modify an app. There is an example in the documentation which explains how to route reads/writes to another database.